I am trying to do a left join using eloquent on a one to many relationship. I would only like to get the row with the highest value in the sort_order column.
So far my query looks like this:
Package::select('packages.*')
->leftJoin('package_routes', 'package_routes.package_id', '=', 'packages.id')
->leftJoin('package_route_items', function($join){
$join->on('package_route_items.package_route_id', '=', 'package_routes.id')
->where(???);
})->...//do more stuff to query here
I'm stuck on the where clause, if I should even be using a where at all.
Since i don't know all the query where conditions i'll try to simplify (you will probably have to adjust column names and syntax
add use DB;
at beginning of your controller/model
$data = DB::table('packages')
->select('packages.*, package_route_items.company_id, package_routes.company_id')
->join('package_routes', 'package_routes.package_id', '=', 'packages.id')
->join('package_route_items.package_route_id', '=', 'package_routes.id')
->orderBy('packages.value_column', 'DESC')->first();
That is what i have understand so far from your description. It's not tested but i think it should work with probably minor editing.
Similar question like yours is here , and here (hope it helps you even more than mine answer).
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.