如何在Laravel中从多个belongstomany关系的枢纽表中筛选出共同的用户。

huangapple go评论93阅读模式
英文:

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

问题

这里有一个表格图
卖家表是主要的表。服务和地区表可以包含特定卖家的多个服务和地区。为此,我使用seller_servicessellerable的中间表,使用belongsToMany()关系来访问服务和地区。

在卖家模型中
public function services()
{
    return $this->belongsToMany('App\Models\Service');
}

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

在服务模型中

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

在地区模型中

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

**现在的问题是....
如果我请求任何服务和任何地区,然后我想要访问那些拥有这些请求服务和地区的卖家,并获取所有符合这两个请求的卖家。
**

我尝试了以下方法....

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

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

$sellers = $service_name->sellers;

$sellers = $area->sellers;

在Blade模板中

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

还尝试了....

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

但是这样的方法无法访问同时拥有服务和地区请求的卖家。我需要访问那些服务和地区都匹配卖家记录的卖家。

英文:

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.

答案1

得分: 0

以下是您提供的代码的翻译部分:

> 尝试这个

    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);
        }
    }

> 然后使用以下方式获取结果:

    在控制器中
    $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();

希望这有所帮助。如果您有任何其他翻译需求,请随时提问。

英文:

> 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();

huangapple
  • 本文由 发表于 2023年3月9日 13:30:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定