0

I need to use withoutGlobalScope() if condition true. I have tried it with when function. But it only work outside of the function.

this is my model

 protected static function boot()
{
    parent::boot();

    static::addGlobalScope('active', function (Builder $builder) {
        $builder->where('vehicles.active', 0);
    });
}

here is the eloquent query

$this->model
        ->when((isset($attbutes->inactive) && $attbutes->inactive==1), function ($q) use ($attbutes) {
            return $q->withoutGlobalScope('active');
        })
        ->get();
Udara Herath
  • 805
  • 2
  • 18
  • 37
  • What value does `$attbutes->inactive` have? Make sure that the `(isset($attbutes->inactive) && $attbutes->inactive==1)` condition returns `true` so that the query inside the `when()` is applied. – Remul Feb 08 '21 at 16:00
  • @Remul I have added `dd` function inside `when` and it's working. But when I use `withoutGlobalScope` it's not working – Udara Herath Feb 08 '21 at 18:24

0 Answers0