Example Analysis of default sorting under Global scope in Laravel Eloquent
This article will explain in detail the example analysis of the default sorting under the global scope in Laravel Eloquent. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Default sorting in global scope
What if you want User::all () to always sort by the name field? You can assign it a global scope. Let's go back to boot (), the method we mentioned above:
Protected static function boot () {parent::boot (); / / sort static::addGlobalScope in name positive order ('order', function (Builder $builder) {$builder- > orderBy (' name', 'asc');}) } this is the end of the article on "sample analysis of default sorting in global scope in Laravel Eloquent". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.