File app/Models/OrderByNameScope.php
(link to Github)
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Builder;
class OrderByNameScope implements Scope
{
public function apply(Builder $builder, Model $model): void
{
$builder->orderBy('name');
}
}
File app/Models/Ingredients/Unit.php
(link to Github)
use Illuminate\Database\Eloquent\Model;
class Unit extends Model
{
//
protected static function boot()
{
parent::boot();
static::addGlobalScope(new OrderByNameScope);
}
//
}
Additional resources on global scopes:
-
Bert Heyman | dev.to
Published on