我有三個表,我想在獲取結果時使用數據透視表數據。與數據透視表的關係
這些表是產品,存儲,store_products,cart_items和他們的田地都低於:
產品(ID,名稱,價格)
店(ID,姓名等)
store_products(STORE_ID,PRODUCT_ID,store_price,store_slug)
cart_items(ID,USER_ID,PRODUCT_ID,S tore_id,數量)
我CartItem模型就像
class CartItem extends Model
{
public function product()
{
return $this->belongsTo('App\Product');
}
public function store()
{
return $this->belongsTo('App\Store');
}
}
當我打電話CartItem::with('product')->get()
,我想它與數據透視表"store_products"
匹配cart_items.store_id and cart_items.product_id
返回產品數據(from product table and store_price, store_slug from store_products)
。
如何通過我的CartItem模型創建此關係?如果可能,只需使用Eloquent ORM函數而不是查詢構建器或原始查詢。
你好,謝謝你的回答,但是這並沒有返回正確的結果。這包括產品數據到CartItem,其中所有商店都位於數據透視表中。我想要做的就是獲得每個CartItem行中定義的特定store_id的數據透視結果 –
試試這個'CartItem :: with(['store.products']) - > get();' –