Comments (3)
I have run into a similar issue and initially was just passing sort_col
and sort_dir
to the query and then in the model filter using a sortCol()
method like:
public function sortCol($col)
{
$this->orderBy($col, $this->input('sort_dir', 'DESC'));
}
Then these methods started getting bloated when ordering by a relation that wasn't joined and having to add each relation's join query.
That eventually turned into:
public function sortCol($col)
{
if(method_exists($this, $method = 'orderBy'.studly_case($col))) {
return $this->$method();
}
$this->orderBy($col, $this->input('sort_dir', 'DESC'));
}
public function orderByClientNumber()
{
$this->join('clients', 'clients.user_id', '=', 'users.id')
->orderBy('clients.work_phone', $this->input('sort_dir', 'DESC'));
}
I'm not sure I like that solution though...
I'm totally open to suggestions on this one as it's been coming up a bit more frequently for me.
from eloquentfilter.
I would define an orderable method on the filter to be able to tune it per model/filter/query and fallback to a default sort. Something like:
protected $sortable = ['created_at', 'updated_at', 'id'];
public function sortable($column)
{
return in_array($column, array_merge($this->getModel()->getFillable(), $this->sortable));
}
Then you can do:
/**
* Sort the Items by a specific column
* Optionally sort in a specific direction
* @param $column
* @return mixed
*/
public function sortBy($column)
{
// Check if pre-defined orderBy method exists to sort by
if (method_exists($this, $method = 'orderBy' . studly_case($column))) {
return $this->$method();
}
// Otherwise order by the column in a specific direction, default DESC
return $this->sortable($column) ? $this->orderBy($column, $$this->input('sort_direction', 'DESC')) : $this->orderBy('created_at', 'DESC')
}
That way you're not adding another query to check for the existence of a column and you are reverting back to your default when the column isn't recognized.
from eloquentfilter.
@Tucker-Eric I like the method you've provided, I actually added an additional if statement around the pure orderBy()
method to ensure that the column being requested exists.
/**
* Sort the Items by a specific column
* Optionally sort in a specific direction
* @param $column
* @return mixed
*/
public function sortBy($column)
{
// Check if pre-defined orderBy method exists to sort by
if (method_exists($this, $method = 'orderBy' . studly_case($column))) {
return $this->$method();
}
// Otherwise order by the column in a specific direction, default DESC
if(Schema::hasColumn('items', $column)){
return $this->orderBy($column, $this->input('sort_direction', 'DESC'));
}
}
Only issue is that the table name needs to be included. Perhaps there is a cleaner way?
from eloquentfilter.
Related Issues (20)
- Combine filter by related and own fields HOT 4
- Postgresql support (case-insensitively) HOT 2
- Filter on append attributes HOT 1
- sorted by table with relation HOT 1
- Breaking change on 2.4.1 from 2.4.0 HOT 3
- Add orWhereLike HOT 2
- How should you handle Laravel Scout integration? HOT 8
- filter by created_at HOT 1
- Your composer is powerful! HOT 1
- Access the other request parameters within the filter function? HOT 2
- Access model class HOT 2
- filtering by date interval HOT 1
- Filter by column and and relation column HOT 1
- Related filter does not accept zero or false value
- $relations setting to create orWhereHas HOT 4
- Can't sort many to many HOT 1
- Can't sort on date HOT 1
- Filter by relationship field and latest fails HOT 3
- Relations with one related method HOT 3
- Filtering by deep relation HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eloquentfilter.