[英]Laravel Nova Model relationship 2 or more models
I'm using the Laravel Nova framework and I'm having difficulty using relationships when it comes to visualization (index, and show methods).我正在使用Laravel Nova 框架,在可视化(索引和显示方法)方面我很难使用关系。
Ex: I have a model called User (id, name address_id), it has a relationship with Address (id, street, state_id), and Address has a relationship with State (id, name), all use belongsTo .例如:我有一个 model 叫做 User (id, name address_id),它和 Address (id, street, state_id) 有关系,Address 和 State (id, name) 有关系,都使用belongsTo 。
How do I show State in the User profile?如何在用户配置文件中显示 State? The way I know of is $ user-> address-> state-> name , but how do I do that with Nova?
我知道的方式是$ user-> address-> state-> name ,但是我该如何使用 Nova 呢? Or would you have to create a state_id field in User as well?
或者您是否还必须在 User 中创建一个 state_id 字段? (that would be awful)
(那会很糟糕)
Nova support callbacks for field value description. Nova 支持字段值描述的回调。 Try this one:
试试这个:
Text::make('state', function($resource) {
// Some code for receiving your state data
return $resource->address->state; // as example
})->exceptOnForms(),
For more details, read this Computed Fields有关更多详细信息,请阅读此计算字段
And this Dynamic Field Methods而这个动态字段方法
You can use displayUsing您可以使用displayUsing
BelongsTo::make('State name', 'address', Address::class)
->displayUsing(function () {
return $this->address->state->name;
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.