Home > Mobile >  Laravel ParseError syntax error, unexpected identifier "Schema", expecting "function&
Laravel ParseError syntax error, unexpected identifier "Schema", expecting "function&

Time:12-13

This question has been asked before but I don't find an answer which solves my problem.

I am working with Laravel 9 framework . I have a class which extends Migration, I edited my class as following code, but after running migrate command I get the above mentioned error.

return new  class extends Migration
{

Schema::create('articles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->string('body');
    $table->integer('user_id');
    $table->timestamps();

});
 }
public function down()
    {
        Schema::dropIfExists('articles');
    }
    } ;

CodePudding user response:

You must put the Schema::create statement in a public function named up

Here's the complete code:

<?php

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

return new class extends Migration
{
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('body');
            $table->integer('user_id');
            $table->timestamps();

        });
    }

    public function down()
    {
        Schema::dropIfExists('articles');
    }
};


  • Related