0
hasManyThroughの関係は、中間モデルがhasManyの関係にあるときにうまく動作することができます。しかし、私のStoreProductモデルのように中間モデルのbelongsToがあればどうでしょうか?これは働くことができるはずですが、私は理解できません。中間モデルにbelongsToを持つManyThrough
私には何が欠けていますか?
また、「StoreProducts」を通じてすべての商品の店舗を取得するにはどうすればよいですか?
class Product extends Model
{
public function storeProducts()
{
return $this->hasMany('App\StoreProduct');
}
public function stores()
{
return $this->hasManyThrough('App\Store', 'App\StoreProduct');
}
}
class StoreProduct extends Model
{
public function store()
{
return $this->belongsTo('App\Store');
}
}
class Store extends Model
{
public function storeProducts()
{
return $this->hasMany('App\StoreProduct');
}
}
実際には、StoreProductをピボットとして使用して、ProductとStoreの間にManyToMany関係を作成して解決したようです。 ここに解決策が見つかりました:https://laracasts.com/discuss/channels/eloquent/hasmanythrough-with-intermediate-belongsto – Emin