Dhanu K
Dhanu K

Reputation: 12403

laravel convert sql to eloquent query

I have a sql query with joins is

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

call to membered function paginate() on array

Upvotes: 0

Views: 1148

Answers (2)

Dhanu K
Dhanu K

Reputation: 12403

Thanks to all I found one solution in eloquent query,i.e.,

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..

Upvotes: 1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

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.

Try this:

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

here $data is an Std Class Object on which you can call the paginate functions.

Reference

Upvotes: 0

Related Questions