Global Query Scope
10
Глобальный scope для модели User для фильтрации только подтвержденных e-mail записей
app/Scopes/VerifiedScope.php
<?php namespace App\Scopes; use Illuminate\Database\Eloquent\Builder;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Scope; class VerifiedScope implements Scope{ /** * Apply the scope to a given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function apply(Builder $builder, Model $model) { $builder->whereNotNull('email_verified_at'); }}
app/Models/User.php
<?php namespace App\Models; use App\Scopes\VerifiedScope;use Illuminate\Database\Eloquent\Model; class User extends Model{ /** * The "booted" method of the model. * * @return void */ protected static function booted() { static::addGlobalScope(new VerifiedScope()); }}