我有這個結構:
model: OrderItem
belongsTo ProductInfo
model: ProductInfo
hasMany OrderItem
hasMany Barcode
model: Barcode
belongsTo ProductInfo
如何在 OrderItem 中建立易于訪問的關系以獲取與相同 product_info_id 匹配的條形碼?product_infos 表當然不是包含外鍵的經典中間表。
我知道這可能是一個非常簡單的問題,因為這種關系也是非常基本的,但我對我在網上找到的所有答案以及在 Laravel 檔案中針對略有不同情況的所有答案感到困惑。Fe hasManyThrough 在這里沒有任何效果。
uj5u.com熱心網友回復:
這種關系是正確的,您可以訪問條形碼,例如
$order->ProductInfo->barcodes
這將回傳一個條形碼陣列
uj5u.com熱心網友回復:
不要使用樞軸模型,試試這個。
OrderItem belongs to many Barcode
那么Barcode模型,
table: barcodes
Barcode belongs to many OrderItem
最后創建 apivot table
php artisan make:migration create_barcodes_order_items_table并為foreign_idsorder_item_id和. 創建兩列barcode_id。
它會自動照顧你的關系,除非你沒有做任何超出模式的事情。
然后您可以獲得所有訂單條碼和條碼的所有訂單。
$order->barcodes
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/422356.html
標籤:
