GithubHelp home page GithubHelp logo

Comments (3)

Tucker-Eric avatar Tucker-Eric commented on May 14, 2024 4

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.

Tucker-Eric avatar Tucker-Eric commented on May 14, 2024 2

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.

philliphartin avatar philliphartin commented on May 14, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.