1
以下$ product返回產品對象。 但我怎麼可以通過foreach訪問關係:數組「標籤」?產品帶有標籤,帶標籤如何訪問關係
當我做$標籤 - >標籤我得到產品表的標籤。
的foreach:
foreach ($product as $tags) {
}
實例:
$product = Product::with('tags')->where('id',$id)->get();
輸出:
Product {#371 ▼
#table: "products"
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:10 [▶]
#original: array:10 [▶]
#relations: array:1 [▼
"tags" => Collection {#399 ▼
#items: array:2 [▼
0 => Tag {#397 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:4 [▶]
#original: array:6 [▶]
#relations: array:1 [▶]
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
Ahaaa,謝謝,工作! – Bas