Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of default sorting under Global scope in Laravel Eloquent

Shulou Source: shulou.com Published: 2022-06-01 04:35:27 09月10日 Update

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.

Tags: Sort function global article example analysis more good practical above content field article method knowledge allocation reference help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info vpn Shulou Technology Xiaomi