【问题标题】:How to use belongsToMany with multiple models in Nova如何在 Nova 中将 belongsToMany 与多个模型一起使用
【发布时间】:2019-07-29 15:59:38
【问题描述】:

我正在尝试将多个模型附加到 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 中的附加功能?

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

【问题讨论】:

标签: php laravel laravel-nova


【解决方案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

【讨论】:

    猜你喜欢
    • 2021-10-10
    • 2019-12-30
    • 1970-01-01
    • 2018-11-27
    • 2010-11-08
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多