【问题标题】:Laravel Nova show Many-to-Many with pivotLaravel Nova 显示带有枢轴的多对多
【发布时间】:2021-07-16 16:13:28
【问题描述】:

我有两张桌子 show_types 和场地。通过多对多关系,我确实创建了一个新的支点,如下所示

public function up()
{
    Schema::create('show_types_venues', function (Blueprint $table) {
        $table->integer('show_types_id')->unsigned();
        $table->foreign('show_types_id')->references('id')->on('show_types');

        $table->integer('venue_id')->unsigned();
        $table->foreign('venue_id')->references('id')->on('venues');
    });
}

并添加到模型中

ShowType.php
    public function venues()
    {
        return $this->belongsToMany(Venue::class, 'show_types_venues', 'show_types_id', 'venue_id');
    }


Venue.php
    public function shows()
    {
        return $this->belongsToMany(ShowType::class, 'show_types_venues', 'venue_id', 'show_types_id');
    }

我正在尝试使这种关系显示在 Nova 上使用

ShowType.php
 public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Name'),
            BelongsToMany::make('Venues', 'venues', Venue::class)
        ];

但我的界面中没有显示任何内容。我怎样才能让它显示一个选择多个,我可以添加/编辑与此 showType 关联的场所? 我需要创建更多“手动”的东西吗?谢谢

【问题讨论】:

    标签: laravel eloquent laravel-nova


    【解决方案1】:

    没有 Nova 的文档,我使用Bejacho's Belongs to Many Fields 做了一个解决方法,我们可以轻松地添加多对多关系,在 Nova 上显示为多重选择器。 与我上面提到的设置相同,我唯一做的就是效仿他们的例子:

    use Benjacho\BelongsToManyField\BelongsToManyField;
    
    public function fields(Request $request){
        return [
            ..., //If you are using with BelongsToMany native Field, put this field after
    
            BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role'),
        ];
    }
    

    完美地满足了我的需要!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2021-08-12
      • 1970-01-01
      相关资源
      最近更新 更多