Search code examples
laravelpivotlaravel-bladehas-and-belongs-to-manylaravel-controller

How to filter common users from multiple belongstomany relationship pivot tables in laravel


There are table diagram Sellers table are main table. Services and Areas tables can contain multiple services and areas for spacific Seller. For this I use seller_services and sellerable Pivot table belongsToMany() relationship to access services and areas.

In Seller Model
    public function services()
    {
        return $this->belongsToMany('App\Models\Service');
    }

    public function areas()
    {
        return $this->morphedByMany('App\Models\Area', 'sellerable');
    }


In Service Model
    public function sellers()
    {
        return $this->belongsToMany('App\Models\Seller');
    }

In Area Model
    public function sellers()
    {
        return $this->morphToMany('App\Models\Seller', 'sellerable');
    }

**Now the problem is.... If I request any service and any area then I want to access those Seller who have those requested services and area. And get all seller who matched this both request. **

I try those....

        $service_name = Service::where('slug', $request->service)->first();

        $area = Area::findOrFail($request->area);
        
        $sellers = $service_name->sellers;

        $sellers = $area->sellers;

In Blade

    @if ($sellers)
        @foreach ($sellers as $seller)
            {{$seller->name}}
        @endforeach
    @endif

And Also Try....

@if ($service_name)
    @foreach ($service_name->sellers and $area_name->sellers as $seller)
         {{$seller->name}}
    @endforeach
@endif

but it this way we can't access sellers who has both request. I need to access sellers where service and area will match this seller records.


Solution

  • Try this

    class Seller extends Model
    {
    
        public function sellerServiceAreas()
        {
            return $this->hasMany(SellerServiceArea::class);
        }
    
       
    }
    
    class Service extends Model
    {
    
    
        public function sellerServiceAreas()
        {
            return $this->hasMany(SellerServiceArea::class);
        }
    
        
    }
    
    class Area extends Model
    {
    
    
        public function sellerServiceAreas()
        {
            return $this->hasMany(SellerServiceArea::class);
        }
    
    }
    
    class SellerServiceArea extends Model
    {
        protected $fillable = ['seller_id', 'service_id', 'area_id', 'price'];
    
        public function seller()
        {
            return $this->belongsTo(Seller::class);
        }
    
        public function service()
        {
            return $this->belongsTo(Service::class);
        }
    
        public function area()
        {
            return $this->belongsTo(Area::class);
        }
    }
    

    And then get the result using :

    In Controller
    $service = Service::where('name', 'Service Name')->first();
    $area = Area::where('name', 'Area Name')->first();
    
    $sellers = Seller::whereHas('sellerServiceAreas', function ($query) use 
    ($service, $area) {
    $query->where('service_id', $service->id)->where('area_id', $area->id);
     })->get();