Laravel Eloquent: How to Order Results of Related Models

Laravel Eloquent: How to order results of related models?

You have a few ways of achieving this:

// when eager loading
$school = School::with(['students' => function ($q) {
$q->orderBy('whateverField', 'asc/desc');
}])->find($schoolId);

// when lazy loading
$school = School::find($schoolId);
$school->load(['students' => function ($q) {
$q->orderBy('whateverField', 'asc/desc');
}]);

// or on the collection
$school = School::find($schoolId);
// asc
$school->students->sortBy('whateverProperty');
// desc
$school->students->sortByDesc('whateverProperty');

// or querying students directly
$students = Student::whereHas('school', function ($q) use ($schoolId) {
$q->where('id', $schoolId);
})->orderBy('whateverField')->get();

How to order results of related models in laravel?

This problem can not be solved this way as i found out. To solve this problem another approach is necessary .

This is how i did this.

$query = Product::with('sizes','images')->whereHas('productCategory',function ($q) use ($cat,$request,$cate){
$q->whereHas('category',function ($q) use ($cat ,$request,$cate){
$q->where('category_slug',$cat);

});

});
$data['result']=$query->get()->toArray();

This can easily be sorted

Laravel: How to order results of related models?

Counting Related
Models

If you want to count the number of results from a relationship without
actually loading them you may use the withCount method, which will
place a {relation}_count column on your resulting models.

So, you can use withCount() and then order the results by the placed column. For example for 'likes' relation:

} elseif ($sortby == 'popular') {
$forums = $forums->withCount('likes')->orderBy('likes_count', 'desc');
}

Laravel Eloquent: How to filter results of related models?

The following should do the trick:

Order::whereHas('customer.country', function($innerQuery) {
$innerQuery->where('countries.name', 'LIKE', 'Uk');
})->get();

Perform order by relationship field in Eloquent

This will sort the subquery. Not the "first query (the product query)".

Basically, your subquery will be:
select * from variants where product_id in (....) order by price, and that is not what you want, right?

<?php 
// ...

$order = $request->sort;

$products = Product::whereHas('variants')->with(['reviews', 'variants' => function($query) use ($order) {
if ($order == 'price') {
$query->orderBy('price');
}
}])->paginate(20);

If you want to sort product +/or variant you need to use join.

$query = Product::select([
'products.*',
'variants.price',
'variants.product_id'
])->join('variants', 'products.id', '=', 'variants.product_id');

if ($order == 'new') {
$query->orderBy('products.created_at', 'DESC');
} else if ($order == 'price') {
$query->orderBy('variants.price');
}

return $query->paginate(20);

Laravel Eloquent: Ordering results of all()

You can actually do this within the query.

$results = Project::orderBy('name')->get();

This will return all results with the proper order.



Related Topics



Leave a reply



Submit