I have a whereHas query which gets data from a pivot table. But on the pivot table i also have a extra column called 'order' and i use this column to make sure the items are ordered in the correct SEQUENCE as it represents a bus route. Ex: Bus/Ferry stops at 1 2 3 4 5 6.
We have a departure and destination point which are represented as 'port_id'.
So for example the below query gives me the correct data but i need to make sure that the 'order' column is always in the correct sequence based on the departure port id and the arrival port id. So if you would search for port_id 14 to port_id 15 it should not return any results as the 'order' column is not connecting those in that way.
This is what i've tried:
$routes = Route::whereHas('ports', function($query) use ($request) {
$query->where('port_id', $request->route['from']['id']);
})
->whereHas('ports', function($query) use ($request) {
$query->where('port_id', $request->route['to']['id']);
})->get();
Thank you.
i think you could make ordering using join, then ordering the result by route_id then by 'order' column:
$routes = Route::whereHas('ports', function($query) use ($request) {
$query->where('port_id', $request->route['from']['id']);
})
->whereHas('ports', function($query) use ($request) {
$query->where('port_id', $request->route['to']['id']);
})
->join('route_port','route_port.route_id','=','routes.id')
->join('ports','route_port.port_id','ports.id')->orderby('route_id')
->orderby('order')->get();
please correct the table names if they were wrong...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.