0

I am adding new column name title in my table tasks. But I am getting an error that this column does not exist in that table. Can anybody help me to resolve that error. Here is my code:

php artisan make:migration add_title_to_tasks_table --table="tasks" and then added this code

Schema::table('tasks', function (Blueprint $table) { 
  $table->string('title');
});

to new table file created

brombeer
  • 8,716
  • 5
  • 21
  • 27
Nisha Sharma
  • 245
  • 1
  • 2
  • 13

3 Answers3

4

To Alter table and add column.

     /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::table('tasks', function (Blueprint $table) {

            $table->string('title')->after('id');

           // to change column datatype or change it to `nullable` 
           // or set default values , this is just example

           $table->string('title')->default('Test')->change(); 

        });
    }

You can refere documentation here, Laravel Migration

Mayank Majithia
  • 1,916
  • 16
  • 21
  • getting this error: "SQLSTATE[HY000]: General error: 1 table tasks has no column named title (SQL: insert into "tasks" ("title", "description", "user_id", "updated_at", "created_at" ▶" – Nisha Sharma Mar 06 '18 at 11:55
  • is your migration run successfully ? match table structure with your requirement. – Mayank Majithia Mar 06 '18 at 11:56
  • In order to modify an existing column, there is a dependency that one has to install.. `Doctrine/DBAL` https://laravel.com/docs/5.6/migrations#modifying-columns – Saiyan Prince Mar 06 '18 at 11:57
  • i installed dependency but still same issue – Nisha Sharma Mar 06 '18 at 12:09
  • @NishaSharma, if your comment is related to my answer/post, please add your comment there.. It is bit confusing for me to recognize as to whether you are referring to my post or Mayank's post – Saiyan Prince Mar 06 '18 at 12:17
  • @SaiyanPrince i tried migartion php artisan make:migration add_title_to_tasks_table --table="tasks" and then added your code to that file and run php artisan migrate but getting error that tasks table already exists – Nisha Sharma Mar 06 '18 at 12:20
  • Can anyone help me – Nisha Sharma Mar 07 '18 at 04:37
  • I know this will work. But i need alternate solution on already installed files – Nisha Sharma Mar 07 '18 at 04:40
3

For those who are new to laravel and looking for a solution, please follow the steps 1). php artisan make:migration add_title_to_tasks_table --table="tasks"

2). Edit the newly created file in the migrations folder and add the below.

public function up() {
    Schema::table('tasks', function (Blueprint $table) {

        $table->string('title')->after('id');

       // to change column datatype or change it to `nullable` 
       // or set default values , this is just example

       $table->string('title')->default('Test')->change(); 

    });
}

3). Run the migrations command.

php artisan migrate

1

Do the following:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('tasks', function (Blueprint $table) {
       $table->string('title')->after('your-column-name');
    });
}

Replace 'your-column-name' with your existing column name.

Save the file. From terminal, execute:

php artisan migrate

The above command will add the column to your table.

You are getting that error because you are not running the migrate command. Whenever you create a migration file, you must execute the above command in order to see the changes in your database table.

Also, if the new column does not exist in the models $fillable property, you will have to add it there as well..

/**
 * The attributes that are mass assignable.
 *
 * @return  array
 */
protected $fillable = [
    'title', // <-- new column name
    // .. other column names
];

Failing to do update the $fillable property will result in MassAssignmentExecption

Hope this helps you out. Happy Coding. Cheers.

Saiyan Prince
  • 3,930
  • 4
  • 28
  • 71