Php 如何在Nova中与多个模型一起使用belongstomy
我正在尝试将多个模型连接到Laravel Nova的透视表Php 如何在Nova中与多个模型一起使用belongstomy,php,laravel,laravel-nova,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(Awar
// 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'),
];
}
在my pivot table user_post_awards中,需要用户id。因此,当我尝试附加一个帖子时,我得到了一个错误。因此,问题是,在向Laravel Nova中的奖励模型附加帖子时,如何设置用户id?是否有可能覆盖Nova中的附加功能
编辑:我正在寻找一种方法,在将帖子附加到奖项时将用户id添加到数据透视表。我通过为数据透视表创建模型和资源解决了您的问题。如果您想创建tri关系,可以打开数据透视资源,然后选择每个值并附加。如果要分离关系,可以选择“从每个模型分离”,也可以选择“从透视资源分离” 参考下面我的代码,快乐编码!注:pivot表“award\u post\u user”的名称是根据Laravel约定命名的。你可以看 可能重复的
// 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