2016-05-20 13 views
1

マイモデルItemsは、Locationsに関連するBuildingsに関連するRoomsに関連しています。Laravel Datatables追加フィールドの並べ替え

はショート:Items belongsToのRooms belongsToのItemControllerの指数関数でBuildings belongsToのLocations

私はItemsのテーブルを表示したいです。私はLaravel Datatablesを使用します。 'シンプルな'テーブルでも動作しますが、カスタムフィールドや追加フィールドの並べ替え/検索の問題に直面しました。これはもちろんテーブルにはないからです。

基本的には、RoomBuildingおよび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); 
} 

追加フィールドのソート/フィルタリングを有効にする最良の方法は何ですか? ありがとうございます。

答えて

0

これは(何の追加フィールドが使用されなかった)私のソリューションです:

public function anyData() 
{ 
    $items = Item::join('rooms', 'items.room_id', '=', 'rooms.id') 
     ->join('buildings', 'rooms.building_id', '=', 'buildings.id') 
     ->join('locations', 'buildings.location_id', '=', 'locations.id') 
     ->select([ 
      'items.id', 
      'items.title', 
      'items.label_number', 
      'items.fibu_number', 
      'rooms.title as room_title', 
      'buildings.title as building_title', 
      'locations.title as location_title' 
     ]); 
    return Datatables::of($items)->make(true); 
} 
関連する問題