Laravel为morphMany集合指定条件

Kev*_*haw 5 polymorphic-associations relationships laravel

我有以下雄辩的关系

class Broker extends Eloquent{

    public function configurable(){
        return $this->morphTo();
    }
}

class ProductConfiguration extends Eloquent{

    public function productConfigurations()
    {
        return $this->morphMany('Excel\Products\ProductConfiguration','configurable');
    }
}
Run Code Online (Sandbox Code Playgroud)

通过这样做,我可以很容易地找到属于Broker的所有ProductConfiguration:

    $broker = Broker::find($id);
    $productConfigurations = $broker->productConfigurations;
Run Code Online (Sandbox Code Playgroud)

我不清楚的是如何指定ProductConfigurations的条件,所以如果我的ProductConfiguration有一个type字段,例如:

    $broker = Broker::find($id);
    $productConfigurations = $broker->productConfigurations->where('type' = 'reg');
Run Code Online (Sandbox Code Playgroud)

检查文档我无法确切地找到如何做到这一点.

Kev*_*haw 4

好吧,肯定只是暂时大脑冻结什么的,就这么简单:

$broker = Broker::find($id);    
$configurations = $broker->productConfigurations()
     ->where('type',$type)
     ->get();
Run Code Online (Sandbox Code Playgroud)