![](/img/trans.png)
[英]Laravel Eloquent multiple whereHas on relationship where column 'order' is higher than the previous whereHas
[英]Laravel Eloquent WhereHas with ORDER column
我有一個 whereHas 查詢,它從 pivot 表中獲取數據。 但是在 pivot 表上,我還有一個名為“訂單”的額外列,我使用此列來確保項目以正確的順序排序,因為它代表了一條公交路線。 例如:巴士/渡輪停靠在 1 2 3 4 5 6。
我們有一個出發點和目的地點,表示為“port_id”。
因此,例如,下面的查詢為我提供了正確的數據,但我需要確保“訂單”列始終根據出發港口 ID 和到達港口 ID 以正確的順序排列。 因此,如果您搜索 port_id 14 到 port_id 15 它不應該返回任何結果,因為“訂單”列沒有以這種方式連接這些結果。
這是我嘗試過的:
$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();
謝謝你。
我認為您可以使用 join 進行排序,然后按 route_id 排序結果,然后按“order”列排序:
$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();
如果表名有誤,請更正...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.