janise liked this thread
Depends if you have model or not, basically:
DB::table('entries')->groupBy('post_id')->get(['post_id', DB::raw('MAX(id) as id')]);
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.
janise liked this reply
->select('MAX(id) AS id');
This might work.
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community