Laravel 5.1 从原始数据库(或只是一个数组)创建集合或关联数组

dan*_*gel 5 php mysql arrays laravel eloquent

给定以下表结构,我想返回一个 Eloquent 集合,或者至少将原始数据库结果转换为关联数组以便于迭代。

建筑物

+----+---------------+
| id | building_name |
+----+---------------+
|  1 | Building 1    |
|  2 | Building 2    |
+----+---------------+
Run Code Online (Sandbox Code Playgroud)

客房

+----+-----------+-------------+
| id | room_name | building_id |
+----+-----------+-------------+
|  1 | Room 1    |           1 |
|  2 | Room 2    |           1 |
|  3 | Room 3    |           2 |
+----+-----------+-------------+
Run Code Online (Sandbox Code Playgroud)

维护日志

+----+-------------------+---------+---------------------+
| id | maintenance_value | room_id |      created_at     |
+----+-------------------+---------+---------------------+
|  1 | Cleaned           |       1 | 2015-09-10 00:54:59 |
|  2 | Cleaned           |       1 | 2015-09-13 01:55:59 |
|  3 | Cleaned           |       2 | 2015-09-09 02:56:59 |
|  4 | Cleaned           |       2 | 2015-09-14 03:57:59 |
|  5 | Cleaned           |       3 | 2015-09-08 04:58:59 |
|  6 | Cleaned           |       3 | 2015-09-15 05:59:59 |
+----+-------------------+---------+---------------------+
Run Code Online (Sandbox Code Playgroud)

现在使用以下原始数据库查询..

$results = DB::select('
  select b.building_name,r.room_name,x.maxdate FROM buildings b
  join rooms r on r.building_id = b.id
  join (select room_id,max(created_at) as maxdate from maintenancelog group by room_id) x on x.room_id=room.id
  having x.maxdate < DATE_SUB(NOW(), INTERVAL 10 DAY)');
Run Code Online (Sandbox Code Playgroud)

返回

Array
(
    [0] => stdClass Object
        (
            [building_name] => Building 1
            [room_name] => Room 1
            [maxdate] => 2015-09-13 01:55:59
        )

    [1] => stdClass Object
        (
            [building_name] => Building 1
            [room_name] => Room 2
            [maxdate] => 2015-09-14 03:57:59 
        )

    [2] => stdClass Object
        (
            [building_name] => Building 2
            [room_name] => Room 3
            [maxdate] => 2015-09-15 05:59:59
        )
Run Code Online (Sandbox Code Playgroud)

是否有给定数组的 Laravel 辅助函数,我可以指定键和分组顺序?有点像,按 building_name,room_name 分组,以便它返回一个集合或关联数组,其中“建筑物”和“房间”作为键,使迭代更容易?我可以建立一个,但这似乎是一项常见的任务。(我确实搜索过)

我正在使用 Eloquent 并为表创建了模型,但是查询太复杂了,我无法弄清楚如何将其转换为 Eloquent 语句。那么即使有一种方法可以使用现有模型创建集合?

(请注意,我已经弄清楚如何使用关系和查询范围从维护日志 -> 房间 -> 建筑物向后工作。但它将建筑物置于关系的最低级别,我希望它是第一个)

Mit*_*ate 0

在建筑类中:

public function rooms(){
    return $this->hasMany('App\Room');
}
Run Code Online (Sandbox Code Playgroud)

在房间类别中:

public function maintenanceLog(){
    return $this->hasMany('App\MaintenanceLog');
}
Run Code Online (Sandbox Code Playgroud)

在维护日志类中:

public function scopeLongest($query){ //you can name it as you like ofc.
    return $query->orderBy('created_at','desc')->first();
}
Run Code Online (Sandbox Code Playgroud)

最后你可以使用:

foreach(Building::all() as $building){
    foreach($building->rooms() as $room){
        echo 'building: ' . $building->building_name . ' room: ' . $room->room_name . ' maxdate: ' . $room->maintenanceLog()->longest()->created_at;
    }
}
Run Code Online (Sandbox Code Playgroud)