Laravel 4: how to order by using Eloquent ORM
If you are using post as a model (without dependency injection), you can also do:
$posts = Post::orderBy('id', 'DESC')->get();
Order by Laravel Eloquent
Because it was a polymorphic relationship I used the following to get it working.
$followers = $user->followers()->orderBy('pivot_created_at', 'desc')->limit(12)->get();
Laravel 5: How to use 'where' or 'orderBy' using eloquent?
You can do it like this:
$ideas = $user->idea()
->where('status', '<>', 'DRAFT')
->orderBy('created_at', 'desc')
->get();
When you use ->idea()
it will start a query.
For more information about how to query a relationship: https://laravel.com/docs/5.7/eloquent-relationships#querying-relations
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.
laravel 5.2 - Model::all() order by
$posts = Post::orderBy('created_at', 'desc')->get();
You can use the orderBy method. Replace the column name with the one you want.
Laravel 4.2 : How to use order by SUM in Laravel
Eloquent way:
$posts = Post::leftJoin('points', 'points.post_id', '=', 'posts.id')
->selectRaw('posts.*, sum(points.points) as points_sum')
->orderBy('points_sum', 'desc')
->paginate(8);
The Query\Builder
way is exactly the same, only the result won't be Eloquent 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();
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);
Related Topics
Calling Function Inside Preg_Replace Thats Inside a Function
Efficiently Pick N Random Elements from PHP Array (Without Shuffle)
How to Store a Variable in PHP Using Session
How to Read Gzip-Ed Response from Stackoverflow API in PHP
Decrementing Alphabetical Values
Inkscape Inside PHP/Apache Doesn't Render Fonts to Png
Using PHP to Execute Multiple MySQL Queries
How to Get All Combinations from Multiple Arrays
Implicit Class Variable Declaration in PHP
PHP Variable Inside Echo 'HTML Code'
Authenticating in PHP Using Ldap Through Active Directory
How to Exclude Certains Columns While Using Eloquent
Mysql: Add Sequence Column Based on Another Field
PHP _Php_Incomplete_Class Object with My $_Session Data