Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding fields to User Eloquent ORM model

I would like to extend the existing User model in Laravel 5.0 to add new columns to the table. How can I do so ?

like image 314
justadev Avatar asked Jan 26 '26 04:01

justadev


1 Answers

  1. Create migration by running command:
php artisan make:migration users_disabled_column

where disabled is name of column you want to add to existed table.

  1. Edit new migration with adding column, here is example:
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UsersDisabledColumn extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function($table) {
            $table->boolean('disabled')->default(false);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function($table) {
            $table->dropColumn('disabled');
        });
    }
}
  1. Execute created migration:

php artisan migrate

  1. Now you can use new column:
$user = User::find($id);
$user->disabled = false;
$user->save();
like image 142
Limon Monte Avatar answered Jan 28 '26 01:01

Limon Monte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!