【问题标题】:Laravel Nova BelongsTo not working when relationship method name and foreign key prefix are different当关系方法名称和外键前缀不同时,Laravel Nova BelongsTo 不起作用
【发布时间】:2019-04-11 15:51:37
【问题描述】:

当关系方法名称和外键前缀不同时,属于关系在我的 Nova 应用程序中不起作用。

我有两个表,event 和 client_location 以及 Models Event 和 ClientLocation

事件模型:

class Event extends Model
{
   public function clientLocation()
   {
       return $this->belongsTo(\App\ClientLocation::class, 'location_id');
   }
}

客户端位置模型:

class ClientLocation extends Model
{
   public function events()
   {
       return $this->hasMany(\App\Event::class, 'location_id');
   }
}

Event 的 Nova Resource 字段方法:

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        BelongsTo::make('clientLocation'),
    ];
}

知道如何处理这个问题吗?

【问题讨论】:

    标签: php laravel laravel-nova


    【解决方案1】:

    BelongsTo::make() 可以接受 3 个参数。

    他们是:

    1. 要显示的名称
    2. 关系名称
    3. 新星资源

    在您的特定情况下,这应该可以工作

    BelongsTo('Events', 'clientLocation', App\Nova\ClientLocation::class)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2015-08-26
      • 2015-12-02
      • 2020-12-14
      • 2015-06-12
      相关资源
      最近更新 更多