Laravel default orderBy

后端 未结 8 1802
离开以前
离开以前 2020-12-24 10:51

Is there a clean way to enable certain models to be ordered by a property by default? It could work by extending the laravel\'s QueryBuilder, but to do so,

8条回答
  •  孤城傲影
    2020-12-24 11:17

    I built a mini Laravel package that can add default orderBy in your Eloquent model.

    Using the DefaultOrderBy trait of this package, you can set the default column you want to orderBy.

    use Stephenjude/DefaultModelSorting/Traits/DefaultOrderBy;
    
    class Article extends Model
    {
        use DefaultOrderBy;
    
        protected static $orderByColumn = 'title';
    }
    

    You can also set the default orderBy direction by setting the $orderByColumnDirection property.

    protected static $orderByColumnDirection = 'desc';
    

提交回复
热议问题