skip to Main Content

I need a help about a easy question, when I use the following code

DB::statement("ALTER TABLE user comment 'User comment'");

this code work in my laravel migration.

But when I use same code laravel documentantion

Schema::create('user', function (Blueprint $table) { $table->comment('User comment'); });

My output in the terminal is as follows

BadMethodCallException

Method IlluminateDatabaseSchemaBlueprint::comment does not exist.

I tried

Schema::create('user', function (Blueprint $table) { $table->comment('User comment'); });

I expected results is succesfull

3

Answers


  1. Chosen as BEST ANSWER

    I was using PHP 7 and the installation of Laravel contemplated version 8, after migrating to PHP 8 it was possible to install Laravel 9 in which the syntax worked


  2. You should define the column first before you use the comment(). Since the comment is a method from the IlluminateDatabaseSchemaColumnDefinition::class you cannot access it using the $table which is an instance of IlluminateDatabaseSchemaBlueprint::class :

    $table->string('name'); // Return type is ColumnDefinition class it means the you can access the comment() after the string(),
    
    $table->string('name')->comment('Name of the user');
    

    Laravel – Column Modifiers

    Login or Signup to reply.
  3. Migration table comments for MySQL and Postgres are released with laravel version 9.14: https://laravel-news.com/laravel-9-14-0

    So, if you want to use this feature, you have to update your laravel version.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search