2016-01-25 43 views
0

我有這樣的關係中獲取數據該城市的特定產品,我如何在產品或城市模型中做到這一點?許多一對多關係laravel

事情是這樣的:

我的產品型號這樣做。 我知道這是錯誤的,但我應該如何做這樣的事情?

public function getProductRateForCity($city_id) 
{ 
    return $this->cities()->where('city_id', $city_id)->where('product_id', $this->id); 
} 
+0

添加' - >獲得()'後,你會看到一些錯誤,並從那裏走。 'return $ this-> cities() - > where('city_id',$ city_id) - > where('product_id',$ this-> id) - > get();' ' –

回答

0
public function getProductRateForCity($city_id) 
{ 
    return $this->cities()->where('product_price_in_city.city_id', $city_id)->get(); 
} 

這爲我做了這份工作。

-1
public function getProductRateForCity($city_id) 
{ 
    return $this->cities()->where('city_id', '=', $city_id)->where('product_id', '=', $this->id)->get(); 
} 

應該這樣做。未經測試。 我從來沒有見過 - > where()只有兩個參數。

0

試試吧,就像在docs

$city = App\Cities::find($city_id); 
$product = $city->products()->where('product_id', $product_id)->get();