Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nishant_nayan's avatar

Laravel Query

How can i convert same mysql query into laravel query

select max(tbl_master_vehicle_specifications_temp_conf.id), tbl_master_vehicle_specifications_sheet.id as bid, tbl_master_vehicle_specifications_sheet.field_title as ft from tbl_master_vehicle_specifications_temp_conf, tbl_master_vehicle_specifications_sheet where tbl_master_vehicle_specifications_temp_conf.primary_id= tbl_master_vehicle_specifications_sheet.id group By bid,ft Order by 1 desc

0 likes
4 replies
Palak27's avatar
Palak27
Best Answer
Level 3

@nishant_nayan

i have used using model eloquent this if this can halp you or you want raw query?

this is not the same you want but kind of


$collection = Orders::join('order_lines', 'order_lines.order_id', '=', 'orders.id')
                                            ->join('products', 'products.id', '=', 'order_lines.product_id')
                                            ->select('products.name', DB::raw('sum(order_lines.quantity) AS Total'))
                                            ->where('orders.user_id', $id)
                                            ->where('orders.created_at', '>=', $newdate)
                                            ->groupBy('order_lines.product_id')
                                            ->orderBy('Total', 'DESC')
                                            ->take(7)
                                            ->get()->toArray();
1 like
nishant_nayan's avatar

Thanks For this amazing answer and i've already solved but @palak27 your answer is also amazing

$primary=DB::table('tbl_master_vehicle_specifications_sheet') ->join('tbl_master_vehicle_specifications_temp_conf','tbl_master_vehicle_specifications_temp_conf.primary_id','=','tbl_master_vehicle_specifications_sheet.id')

                ->select('tbl_master_vehicle_specifications_sheet.field_title as field_title' ,'tbl_master_vehicle_specifications_sheet.id as id','tbl_master_vehicle_specifications_sheet.id',DB::raw(' max(tbl_master_vehicle_specifications_temp_conf.id) as mid'))
                ->groupBy('tbl_master_vehicle_specifications_sheet.id')
                ->groupBy('tbl_master_vehicle_specifications_sheet.field_title')
                
                ->orderBy('mid',' desc')
            ->get();

Please or to participate in this conversation.