Ad
Combining Relationship Sorting And Filtering
I have two models: Item and Category. The Item model has a category_id field which is a foreign key of the category.
On the search page I am currently performing filtering. This is done via a series of where()
clauses, for example
if($request->input('location_id', "") != "") {
$query->where('location_id', '=', $request->input('location_id'));
}
These are contained within the scope search(), so they are called like this:
$results = Item::search($request)->get();
I now want to apply sorting to the results, firstly by the name
column of the category, then by the product_number
column on the items table.
How would I go about doing this without interfering with the filtering in the search
scope?
Ad
Answer
Just add multiple calls to orderBy():
$results = Item::search($request)
->join('categories', 'category_id', '=', 'categories.id')
->select('items.*')
->orderBy('categories.name')
->orderBy('product_number')
->get();
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad