【发布时间】:2016-05-20 11:17:13
【问题描述】:
我的模型Items与Rooms相关,Buildings与Locations相关。
短:ItemsbelongsTo RoomsbelongsTo BuildingsbelongsTo Locations
在ItemController的索引函数中,我想显示一个Items的表格。我使用 Laravel 数据表。它适用于“简单”表,但我遇到了排序/搜索自定义/附加字段的问题,因为它们当然不在表中。
基本上我想在显示表中为每个Item 加入Room、Building 和Location 名称。
这是我的Items 模特:
protected $appends = [
'building_title',
'location_title',
'room_title',
];
public function getLocationTitleAttribute() {
return $this->room->building->location->title;
}
public function getBuildingTitleAttribute() {
return $this->room->building->title;
}
public function getRoomTitleAttribute() {
return $this->room->title;
}
这是我的控制器:
public function anyData()
{
return Datatables::of(Item::query())->make(true);
}
为附加字段启用排序/过滤的最佳方法是什么? 感谢您的阅读。
【问题讨论】:
标签: php mysql laravel datatables eloquent