Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

withoutGlobalScope is not working in relation hasMany in Laravel Model

My Scope:

<?php

namespace App\Scopes;

use Auth;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class InventorySeriesScope 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
     */
    // withoutGlobalScope(ClinicScope::class)
    public function apply(Builder $builder, Model $model)
    {
        $table = $model->getTable();
        $builder->where($table . '.is_used',0);
         
    }
}

InventorySeries Model where scope is used:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Scopes\InventorySeriesScope;

class InventorySeries extends Model
{
    use SoftDeletes;

    protected $table="inventory_series";
    protected $primarykey="id";
    protected $guarded=['id'];

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope(new InventorySeriesScope);
    }

}

My model where i am using withoutGlobalScope:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class GatePassOutwardEntryChild extends Model
{
    use SoftDeletes;

    protected $table = 'gatepass_outward_entry_child';
    protected $PrimaryKey = 'id';
    protected $guarded = ['id'];

    public function inventoryseries()
    {
        return $this->hasMany('App\Models\InventorySeries','gatepass_outward_child_id','id')->withoutGlobalScope(InventorySeriesScope::class);
    }
}

My controller where I want my data withoutGlobalScope which gives data including global scope

$data['child'] = GatePassOutwardEntryChild::with('inventoryseries')->get();
dd($data['child'][0]);

In this as a result I am getting data for inventoryseries with is_used as 0 but I have some records with is_used as 0 and 1 both, I want all that records not only with 0

>Solution :

give full path in your model instead of using class like

public function inventoryseries()
{
   return $this->hasMany('App\Models\InventorySeries','gatepass_outward_child_id','id')->withoutGlobalScope("App\Scopes\InventorySeriesScope");

}

It works for me !!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading