Take a look at the hydrate and hydrateRaw methods on your eloquent model. Should do what you want :)
Oct 21, 2015
4
Level 6
Raw Query but Map to my Model??
Hello! I have a table of inventory levels. It integrates with an RFID reader that pushes info to my database mapped by the (basically) id, pusher_id, item_count, and created_at.
I use the following line to get an array of the latest inventory levels by customer.
$inventories = DB::select(DB::raw('SELECT inv_1.*
FROM inventories inv_1
WHERE inv_1.created_at = (
SELECT MAX(inv_2.created_at)
FROM inventories inv_2
WHERE inv_2.pusher_id = inv_1.pusher_id
) AND client_id = '. $this->user->client_id .';'));
The Problem is, this is returned as an array when I would like it as an eloquent model collection so I can utilize the relationships built in.
How can I rewrite this query using Inventory::...
Thanks!
Level 4
3 likes
Please or to participate in this conversation.