我计划使用Laravel和Nova开发一个Web应用程序 . Nova是最近推出的Laravel的CMS软件包 . 由于这是新技术,我遇到了使用它的问题 . 我无法为资源中的外键声明一个字段 .

我创建了一个名为Post的新模型,运行artisan命令来制作模型,这是Post迁移类的定义 .

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');
    }
}

然后,我为它运行此命令创建了一个资源 .

php artisan nova:resource Post

当我检查Nova管理仪表板时,我可以看到添加了Post资源的菜单项 .

enter image description here

然后在Post资源的fields方法中,我为表单scald折叠添加了这段代码 .

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

当我从Nova仪表板UI创建新帖子时,我可以看到这些字段 . 当我创建时,它给出了一个错误,指出需要用户ID . 所以,我试着像这样指定用户字段 .

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()
        ];
    }

当我再次创建一个新的Post时,会抛出另一个错误,即“调用没有globalScopes的成员函数” .

enter image description here

我该如何解决?