Laravel default orderBy

后端 未结 8 1790
离开以前
离开以前 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:30

    A note from my experience, never to use orderBy and GroupBy such term on global scope. Otherwise you will easily face database errors while fetching related models in other places.

    Error may be something like:

    "ORDER BY "created_at" is ambiguous"

    In such case the solution can be giving table name before column names in your query scope.

    "ORDER BY posts.created_at"
    

    Thanks.

提交回复
热议问题