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
PHP Syntax for Arrays: Different Behaviour Between PHP Versions
Swift Httppost Data Not Inserting to MySQL Database
Mysqli Prepared Statements with in Operator
Get Orders Total Purchases Amount for the Day in Woocommerce
Php: Variable Not Working Inside of Function
Why Are Floating Point Numbers Printed So Differently
How to Access a Deep Object Property Named as a Variable (Dot Notation) in PHP
Mysql_Num_Rows() Expects Parameter 1 to Be Resource, Boolean Given In
How to Open a Link in Private Browsing Mode
Are the PHP Preg_Functions Multibyte Safe
Split Alphanumeric String Between Leading Digits and Trailing Letters
How to Pass an Array in Get in PHP
How to Find the Socket Transport "Ssl" - Did You Forget to Enable It When You Configured PHP
How to Parse a Date String in PHP
Set Precision for a Float Number in PHP
Select Count() VS MySQL_Num_Rows();
How to Make a Multidimensional Array Unique
Warning: Preg_Match() [Function.Preg-Match]: Unknown Modifier '/'