如何在Nova中对多个模型使用belongsToMany

时间:2019-07-29 15:59:38

标签: php laravel laravel-nova

我正在尝试将多个模型附加到Laravel Nova中的数据透视表上。

// Post model
public function awards()
{
return $this->belongsToMany(Award::class, 'user_post_awards')
            ->withTimestamps();
}

// User model
public function awards()
{
return $this->belongsToMany(Award::class, 'user_post_awards')
            ->withTimestamps();
}

// Award model
public function users()
{
return $this->belongsToMany(User::class, 'user_post_awards')
            ->withTimestamps();
}

public function posts()
{
return $this->belongsToMany(Post::class, 'user_post_awards')
            ->withTimestamps();
}

// Nova resource
public function fields(Request $request)
{
return [
    Text::make('name')->sortable(),
    belongsToMany::make('Users', 'users', 'App\Nova\User'),
    belongsToMany::make('Posts', 'posts', 'App\Nova\Post'),
];
}

在我的数据透视表user_post_awards中,需要user_id。因此,当我尝试附加帖子时,出现错误。所以问题是,在Laravel Nova中将Post附加到Award模型时,如何设置user_id?有没有办法覆盖Nova中的attach函数?

编辑:当我在奖励上附加帖子时,我正在寻找一种将user_id添加到数据透视表中的方法。

1 个答案:

答案 0 :(得分:1)

我通过为数据透视表创建模型和资源解决了您的问题。在要创建三关系的情况下,您可以打开数据透视表资源,然后选择每个值并附加。如果要分离关系,则可以从每个模型或从数据透视表资源中选择分离!

请在下面引用我的代码,祝您编程愉快!注意:数据透视表“ award_post_user”的名称是我根据Laravel约定命名的。您可以阅读here

// database\migrations\2019_07_30_000133_create_posts_table.php
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->string('title',500);
        });
    }

// database\migrations\2019_07_30_000243_create_awards_table.php
    public function up()
    {
        Schema::create('awards', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->string('name',100);
        });
    }

// database\migrations\2019_07_30_000741_create_award_post_user_table.php
    public function up()
    {
        Schema::create('award_post_user', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->unsigned();
            $table->bigInteger('post_id')->unsigned()->nullable();
            $table->bigInteger('award_id')->unsigned()->nullable();
            $table->timestamps();
        });
    }

// app\User.php
    public function awards()
    {
        return $this->belongsToMany(Award::class,'award_post_user');
    }

// app\Post.php
    public function awards()
    {
        return $this->belongsToMany(Award::class,'award_post_user');
    }

// app\Award.php
class Award extends Model
{
    public function posts()
    {
        return $this->belongsToMany(Post::class,'award_post_user');
    }

    public function users()
    {
        return $this->belongsToMany(User::class,'award_post_user');
    }
}

// app\AwardPostUser.php
class AwardPostUser extends Model
{
    protected $table = 'award_post_user';
}

// app\Nova\User.php
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            ...
            BelongsToMany::make('Awards'),
        ];
    }

// app\Nova\Post.php
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Title'),
            BelongsToMany::make('Awards')
        ];
    }

// app\Nova\Award.php
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Name'),
            BelongsToMany::make('Posts','posts',\App\Nova\Post::class),
            BelongsToMany::make('Users','users',\App\Nova\User::class),
        ];
    }

// app\Nova\AwardPostUser.php
    public static $model = 'App\AwardPostUser';

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Select::make('User','user_id')
            ->options(\App\User::pluck('name','id')->toArray())
            ->displayUsingLabels(),

            Select::make('Post','post_id')
            ->options(\App\Post::pluck('title','id')->toArray())
            ->displayUsingLabels(),

            Select::make('Award','award_id')
            ->options(\App\Award::pluck('name','id')->toArray())
            ->displayUsingLabels(),
        ];
    }


  [1]: https://github.com/alexeymezenin/laravel-best-practices