【发布时间】:2016-12-28 22:23:22
【问题描述】:
我在帖子中定义了许多标签。 我将帖子循环到每个帖子,并尝试检查标签是否包含特定名称 - 例如“汽车”。
$post->tags
循环还给我
Collection {#346 ▼
#items: array:2 [▼
0 => Tag {#353 ▼
#fillable: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:4 [▶]
#original: array:8 [▶]
#relations: array:1 [▶]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => Tag {#352 ▶}
]
}
如何查看标签名称?
【问题讨论】:
-
执行此操作
dd($post->tags->toarray())这将在数组中显示结果(以便更容易可视化输出,这仅用于测试),然后您只需执行 foreach 迭代每个元素就像@mannil 写的那样。 -
感谢您的提示。它很好地返回数组。
标签: php laravel collections