1
我試圖用Eloquent在多重關係的最後一個表上找到列的最大值。Laravel 5.1用()和max()使用多重關係多數人關係
給出下面的表格結構。
建築
+----+---------------+
| id | building_name |
+----+---------------+
| 1 | Building 1 |
| 2 | Building 2 |
+----+---------------+
客房
+----+-----------+-------------+
| id | room_name | building_id |
+----+-----------+-------------+
| 1 | Room 1 | 1 |
| 2 | Room 2 | 1 |
| 3 | Room 3 | 2 |
+----+-----------+-------------+
maintenancelog
+----+-------------------+---------+---------------------+
| id | maintenance_value | room_id | timestamp |
+----+-------------------+---------+---------------------+
| 1 | Cleaned | 1 | 2015-09-06 00:54:59 |
| 2 | Cleaned | 1 | 2015-09-06 01:55:59 |
| 3 | Cleaned | 2 | 2015-09-06 02:56:59 |
| 4 | Cleaned | 2 | 2015-09-06 03:57:59 |
| 5 | Cleaned | 3 | 2015-09-06 04:58:59 |
| 6 | Cleaned | 3 | 2015-09-06 05:59:59 |
+----+-------------------+---------+---------------------+
我想看看是否有可能生成將檢索建築物名稱的雄辯的發言,房間名稱和僅最後一個維護日誌日期值。
下面的工作給我一個所有值的集合。
$buildings = Building::with('rooms.maintenancelog')->get();
但這樣的錯誤了,它看起來像它試圖在建築物表調用MAX(maintenancelog.timestamp)..
$buildings = Building::with('rooms.maintenancelog')->max('maintenancelog.timestamp')->get();
返回錯誤:
.....(SQL: select max(`maintenancelog`.`timestamp`) as aggregate from `buildings`)
上午我只是從雄辯中提出太多問題,應該只使用基本查詢構建器