Transform Mysql query into Eloquent (Laravel)

Asked

Viewed 155 times

0

I have a simple query on MySQL, the query lists all the data of a table making a Group By in the field grupo_id and listing the latest data from Group By based on the created_at. In short, he lists the last data of the group.

The query is simple, I need to turn it into a query Eloquent, follows the code:

select *
from fundoloja_desocupacao
WHERE created_at IN (
    SELECT MAX(created_at)
    FROM fundoloja_desocupacao
    GROUP BY grupo_id
)
  • Do you have the class or are you using DB? Which version is Laravel?

1 answer

1


Basically:

$subSelect = ' created_at IN ';
$subSelect .= ' (SELECT MAX(created_at) FROM fundoloja_desocupacao GROUP BY grupo_id) ');
$result = DB::table('fundoloja_desocupacao')
    ->whereRaw($subSelect)
    ->get();

Reference: Query Builder

Browser other questions tagged

You are not signed in. Login or sign up in order to post.