【发布时间】:2020-06-02 10:16:24
【问题描述】:
全新带背包。我在官方网站上搜索并google了它,但没有找到答案
在 laravel 7 中,使用 Backpack 4.1
我的数据模型是:客户有很多地址
关系在客户模型中配置:
public function addresses()
{
return $this->hasMany(\App\Models\Address::class, 'user_id');
}
关系在地址模型中配置:
public function customer()
{
return $this->belongsTo(\App\Models\Customer::class);
}
public function country()
{
return $this->belongsTo(\App\Models\Country::class);
}
public function address_type()
{
return $this->belongsTo(\App\Models\AddressType::class);
}
在我的客户显示页面中,我想在表格中显示所有客户地址,就在客户详细信息下方。 所以在我的 CustomerCrudController 中,我实现了这个方法:
protected function setupShowOperation()
{
$this->crud->set('show.setFromDb', false);
$this->crud->addColumn(['name' => 'name', 'type' => 'text', 'label' => __('models/customers.fields.name'), ]);
$this->crud->addColumn(['name' => 'email', 'type' => 'email', 'label' => __('models/customers.fields.email'), ]);
$this->crud->addColumn([
'name' => 'addresses',
'label' => __('models/addresses.plural'),
'type' => 'table',
'columns' => [
'address_type_id' => __('models/addresses.fields.address_type'),
'address_type.name' => __('models/addresses.fields.address_type'),
'address1' => __('models/addresses.fields.address1'),
'address2' => __('models/addresses.fields.address2'),
'city' => __('models/addresses.fields.address2'),
'postal_code' => __('models/addresses.fields.address2'),
'country.name' => __('models/countries.singular'),
],
]);
}
当我进入我的页面时:/admin/customer/3/show, 在我的调试栏中,我看到了查询如何加载地址
select * from `addresses` where `addresses`.`user_id` = 3 and `addresses`.`user_id` is not null
我用数据库中数据的相应行数渲染了表格,但行是空白的。 这是正确的方法吗?什么是正确的参数? 有没有办法显示带有操作按钮(显示条目、编辑)的表格 - 与列表视图中的一样?
应该以其他方式实现吗?
希望我很清楚。 谢谢
【问题讨论】: