Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.

janise liked this thread

1

Depends if you have model or not, basically:

DB::table('entries')->groupBy('post_id')->get(['post_id', DB::raw('MAX(id) as id')]);
Last updated 2 years ago.
0

Thanks for your reply. Would it also be possible to do this without DB::raw?

It's hard to see the benefits of the query builder compared to normal PDO queries if I got to use raw SQL anyway.

Last updated 2 years ago.

janise liked this reply

1
->select('MAX(id) AS id');

This might work.

Last updated 2 years ago.
0

Actually most times you will have problems to order it right. Only way i find that results well is:

$query = Stock::where('account_id', $account_id);

$sub = Stock::orderBy('microtime', 'desc'); #THIS IS MY ORDER FIELD. YOU CAN USE ID IF ITS OK FOR YOU IN MY CASE NOT

 $query->from(DB::raw("({$sub->toSql()}) as sub"))
            ->where('data_movimento','<=', $data_movimento)
            ->whereIn('product_id', $products)
            ->with('product','launch','armazem')
            ->groupBy('armazem_id')->get();

Its a query with a subquery. I try a lot of diferent aprouchs none work.

Last updated 8 years ago.
0

thank you @zaalbarxx, your answer still helped someone after three years. thumbs up!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Yannik yannik Joined 16 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.