"Call to a member function without globalScopes on null" in Laravel and Nova

Wai*_*ein 1 php laravel laravel-nova

I am planning to develop a web application using Laravel and Nova. Nova is a CMS package for Laravel introduced very very recently. Since it is the new technology, I am having an issue with using it. I cannot declare a field for the foreign key in the resource.

I created a new model called, Post running the artisan command to make model and this is the definition of the Post migration class.

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->string("title");
            $table->text('content')->nullable();
            $table->unsignedInteger('user_id');
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}
Run Code Online (Sandbox Code Playgroud)

Then, I created a resource for it running this command.

php artisan nova:resource Post
Run Code Online (Sandbox Code Playgroud)

When I check the Nova admin dashboard, I can see that the menu item for Post resource is added.

在此处输入图片说明

Then in the fields method of the Post resource, I added this code for the form scald folding.

public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Title')->rules('required')->sortable(),
            Textarea::make('content')->rules('required')->hideFromIndex()
        ];
    }
Run Code Online (Sandbox Code Playgroud)

When I create a new Post from the Nova dashboard UI, I can see the fields. When I create, it is giving me an error saying that User id is required. So, I tried to specify the User field as well like this.

public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            BelongsTo::make('User')->rules('required'),
            Text::make('Title')->rules('required')->sortable(),
            Textarea::make('content')->rules('required')->hideFromIndex()
        ];
    }
Run Code Online (Sandbox Code Playgroud)

When I create a new Post again, another error is thrown which is "call to a member function without globalScopes ".

在此处输入图片说明

How can I fix it?

小智 17

我有同样的问题,因为我忘记在我的模型中返回关系

我首先有:

public function user()
{
    $this->belongsTo(User::class);
}
Run Code Online (Sandbox Code Playgroud)

代替:

public function user()
{
    return $this->belongsTo(User::class);
}
Run Code Online (Sandbox Code Playgroud)

  • 我也是!很棒的收获:) (2认同)