File app/Scopes/AvailableScope.php
(link to Github)
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class AvailableScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->available();
}
}
File app/Product.php
(link to Github)
use App\Scopes\AvailableScope;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//
protected static function booted()
{
static::addGlobalScope(new AvailableScope);
//
}
//
public function scopeAvailable($query)
{
return $query->where('status', 'available');
}
//
}
Additional resources on global scopes:
-
Bert Heyman | dev.to
Published on