简体   繁体   English

laravel将sql转换为雄辩的查询

[英]laravel convert sql to eloquent query

I have a sql query with joins is 我有一个联接的SQL查询是

select t.id,t.trip_id,c.containerid,s.shipment_name,s.source_location_name,s.destination_location_name,ss.status_name,st.status_name as status_type from 
(select T.* from(SELECT * FROM tbl_trip_status ORDER BY id DESC) T group by T.trip_id) ts
JOIN tbl_trips t ON t.id=ts.trip_id
JOIN tbl_container c ON c.id=t.container_id 
JOIN tbl_shipment s ON s.id=t.shipment_id 
JOIN tbl_statuses ss ON ss.id=ts.status_id
JOIN tbl_status_types st ON st.id=ss.status_type
where t.status=1 and t.user_id in (1,2,3,4,5,6,14)  
ORDER BY `t`.`trip_id` ASC

I need this query in eloquent mode while am trying to call paginate() with this raw query, it showing 当我尝试使用此原始查询调用paginate()时,我需要雄辩的查询模式,它显示

call to membered function paginate() on array 在数组上调用成员函数paginate()

I think the issue is, after getting the data from your query, you have converted that Std Class Object to an array that's why the error you are getting. 我认为问题是,在从查询中获取数据后,您已将该Std类对象转换为数组,这就是您获取错误的原因。

Try this: 尝试这个:

$data = DB::select(DB::raw('your query'));

here $data is an Std Class Object on which you can call the paginate functions. $data是一个Std Class Object ,可以在其上调用paginate函数。

Reference 参考

Thanks to all I found one solution in eloquent query,ie, 多亏了我,我在雄辩的查询中找到了一个解决方案,即

DB::table('tbl_trips as t')->select('t.id','t.trip_id','c.containerid','s.shipment_name','s.source_location_name','s.destination_location_name','ss.status_name','st.status_name as status_type')
                    ->join(DB::raw('(select T.* from(SELECT * FROM tbl_trip_status ORDER BY id DESC) T group by T.trip_id) ts'),'ts.trip_id', '=', 't.id')
                    ->join('tbl_container as c', 'c.id', '=', 't.container_id')
                    ->join('tbl_shipment as s', 's.id', '=', 't.shipment_id')
                    ->join('tbl_statuses as ss', 'ss.id', '=', 'ts.status_id')
                    ->join('tbl_status_types as st', 'st.id', '=', 'ss.status_type')
                    ->where('t.status','=',"1")
                    ->where('t.id', '=', 'ts.trip_id')
                    ->whereIn('t.user_id',$subusrs)
                    ->paginate(10);

Thats all.. 就这样..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM