Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new column in Laravel's default Users table?

Tags:

laravel

I modified the default laravel's user table and I did that by creating my own migration.

Here's my migration sample:

 public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('username', 15)->unique()->after('id');
            $table->string('lastname', 15)->unique()->after('username');
            $table->string('firstname', 15)->unique()->after('lastname');
            $table->string('contact')->nullable()->after('email');
            $table->date('birthday')->after('contact');
            $table->tinyInteger('status')->default(1)->after('birthday');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('username');
            $table->dropColumn('lastname');
            $table->dropColumn('firstname');
            $table->dropColumn('contact');
            $table->dropColumn('birthday');
            $table->dropColumn('status');
        });
    }

And I removed the column name in the default laravel table so it looks like this:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

I checked the values from my form in the AuthController and I dont have an issue in getting the values. Here's what I added in the AuthController

protected $username = 'username'; //choose username instead of email
protected $redirectPath = '/dashboard'; //if successful login
protected $loginPath = 'auth/login'; //if not login
protected $redirectAfterLogout = 'auth/login'; //redirect after login

/**
 * Create a new authentication controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'getLogout']);
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'username'  => 'required|min:8|max:16|unique:users',
        'lastname'  => 'required',
        'firstname' => 'required',
        'contact'   => 'required',
        'birthday'  => 'date_format: Y-m-d',
        'email'     => 'required|email|max:255|unique:users',
        'password'  => 'required|confirmed|min:6',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{

    //dd($data); -- I can get all the values here

    // I added my own column in this part to save. Is it correct?
    return User::create([
        'username'  =>  $data['username'],
        'lastname'  =>  $data['lastname'],
        'firstname' =>  $data['firstname'],
        'birthday'  =>  $data['birthday'],
        'contact'   =>  $data['contact'],
        'email'     =>  $data['email'],
        'status'    =>  1,
        'password'  =>  bcrypt($data['password']),
    ]);
}

And when I try to save the data I can only save these columns

mysql> select * from users \G;
*************************** 1. row ***************************
            id: 1
      username:
      lastname:
     firstname:
         email: [email protected]
       contact: NULL
      birthday: 0000-00-00
        status: 1
      password: $2y$10$3NkmqZaje4MKzheqPZKbhOIGD7ZlqYRfIP6DJZz4zb4gXVNvFsv2W
remember_token: PCYczF2Y9ts97TvbDOLsiXO5hxkekkxysmMYuIdN5MsaIY8TnroEof6d2jVM
    created_at: 2015-09-17 06:56:43
    updated_at: 2015-09-17 07:00:35
1 row in set (0.00 sec)

ERROR:
No query specified

How can I add my new column in the database?

Ok that's all I hope you can help me.

like image 718
Jerielle Avatar asked Sep 17 '15 07:09

Jerielle


2 Answers

Ok I found the answer. I need to put my own custom column in the fillable content:

 protected $fillable = ['username', 'lastname', 'firstname', 'contact',  'email', 'birthday', 'status', 'password'];
like image 31
Jerielle Avatar answered Nov 15 '22 06:11

Jerielle


You can not edit the users table with this way first, go to the terminal and write this command :

php artisan make:migration add_new_fields_to_users_table

and click enter. After that, go to the migration file and in the add_new_fields_to_users_table.php and write your new columns

 $table->string('username', 15)->unique()->after('id');
        $table->string('lastname', 15)->unique()->after('username');
        $table->string('firstname', 15)->unique()->after('lastname');
        $table->string('contact')->nullable()->after('email');
        $table->date('birthday')->after('contact');
        $table->tinyInteger('status')->default(1)->after('birthday');

Then , go to the terminal and do this:

php artisan migrate

and finally go to the User model and add the name's of your new table to the fillable variable

protected $fillable = [
    'name', 'email', 'password','username',
    'lastname','firstname','contact','birthday','status'
];

I hope this will help you

like image 92
Ahmed Rakha Avatar answered Nov 15 '22 06:11

Ahmed Rakha