【问题标题】:How to create belongToMany with some fields?如何使用某些字段创建belongToMany?
【发布时间】:2021-09-24 23:15:38
【问题描述】:

请帮忙。 我不明白。如何将字段添加到关系 belongsToMany Laravel nova。 版本 Nova - 3.27(今天最后)。即使没有带有枢轴的示例,字段也是如此。 示例和示例 2 字段仅显示编辑页面。查看和索引!不显示。

BelongsToMany::make('Product in cart', 'productPrices', ProductPrice::class)->fields(function () {
            return [
                Text::make('Example')->showOnIndex(true),
            Text::make('Example2')->showOnIndex(true)->displayUsing(function($field) {}),
            ];
        }),

【问题讨论】:

    标签: php laravel laravel-nova


    【解决方案1】:

    为了使数据透视字段显示在资源的 Detail 页面上,必须在 both 资源上定义 BelongsToMany 字段。您应该将关系的逆添加为 BelongsToMany 以及 ProductPrice Nova Resource。

    BelongsToMany 字段永远不会显示在 Index 页面上。但是有一个解决方法,您可以创建一个仅存在于索引上的字段,如下所示:

    Textarea::make('Products in cart', function () {
        return $this->productPrices->map(function ($productPrice) {
          // build a correct string here
          return sprintf("%s - %s <br/>", $productPrice->name, $productPrice->amount); 
        })->join('');
    })->onlyOnIndex();
    

    以上代码可能需要根据您的需要进行调整。但它只会显示在索引页面上。

    【讨论】:

      猜你喜欢
      • 2011-07-06
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 2011-07-28
      相关资源
      最近更新 更多