php - Laravel 5.1 create collection or associative array from raw DB (or just an array) -


given following table structure, want return either eloquent collection or @ least convert raw db result associative array easier iteration.

buildings

+----+---------------+ | id | building_name | +----+---------------+ |  1 | building 1    | |  2 | building 2    | +----+---------------+ 

rooms

+----+-----------+-------------+ | id | room_name | building_id | +----+-----------+-------------+ |  1 | room 1    |           1 | |  2 | room 2    |           1 | |  3 | room 3    |           2 | +----+-----------+-------------+ 

maintenancelog

+----+-------------------+---------+---------------------+ | 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 | +----+-------------------+---------+---------------------+ 

now using following raw db query..

$results = db::select('   select b.building_name,r.room_name,x.maxdate buildings b   join rooms r on r.building_id = b.id   join (select room_id,max(created_at) maxdate maintenancelog group room_id) x on x.room_id=room.id   having x.maxdate < date_sub(now(), interval 10 day)'); 

returns

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         ) 

is there laravel helper function given array, can specify keys , grouping orders? sort of like, group building_name,room_name return either collection or associative array "buildings" , "rooms" keys make iteration easier? build one, seems common task. (i did search)

i'm using eloquent , have models made tables, query complicated me figure out how turn eloquent statement. if there way create collection using existing models?

(note have figured out how use relationships , query scopes work backwards maintenance log -> rooms -> buildings. puts buildings @ lowest level of relationship, , want first)

in building class:

public function rooms(){     return $this->hasmany('app\room'); } 

in room class:

public function maintenancelog(){     return $this->hasmany('app\maintenancelog'); } 

in maintenancelog class:

public function scopelongest($query){ //you can name ofc.     return $query->orderby('created_at','desc')->first(); } 

finally can use:

foreach(building::all() $building){     foreach($building->rooms() $room){         echo 'building: ' . $building->building_name . ' room: ' . $room->room_name . ' maxdate: ' . $room->maintenancelog()->longest()->created_at;     } } 

Comments