If you find this package helpful please give it a start on Github. If you don't want to use the package, you can add this code to your Eloquent model. This package is has a trait DefaultOrderBy that adds a default_order_by global scope to the boot() method of your Eloquent model. * Default Sorting Order */ 'order_by' => 'asc', Now you can update the configuration file as you desire. php artisan vendor:publish -provider= "Stephenjude\DefaultModelSorting\DefaultModelSortingServiceProvider" -tag= "config" To set the global default $orderB圜olumnDirection property, publish the package configuration file. protected static $orderB圜olumnDirection = 'asc' This property is set to asc as the default value. You can chain this method with a where () call for even more fine-grained results. You can also set the $orderB圜olumnDirection property. Although you could use the sortDesc () method to reorder the collection within the list->links call, using the orderBy () method provides more flexibility and allows you to include additional filtering conditions later. Open the routes/web.php file in your code editor: routes/web.php. Now your Article model queries will be ordered by title column in ascending order. If you examine the main application route file, where the index route is defined, you’ll notice that the current application code queries all links using a Link::all () call. Protected static $orderB圜olumn = 'title' use Stephenjude/ DefaultModelSorting/ Traits/ DefaultOrderBy You will use the DefaultOrderBy trait and set the $orderB圜olumn property inside your Article model. Using the DefaultOrderBy trait of this package, you can set the default column you want to sort by.įor example, if you want to set the default order column for Article model (assuming you are building a blog). composer require stephenjude/ default-model-sorting get() //sorted by the title //using default-model-sortingĪrticle::all() // sorted by the title Installationįirst you have to install this package via composer. With this package, you can set default order by in your Eloquent model so you don't have to call the orderBy Eloquent builder. By default, Laravel Eloquent models return queries that are ordered by the id column of the model table. Default Model Sorting package is a mini Laravel package I published recently.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |