【发布时间】:2021-02-09 02:08:16
【问题描述】:
我正在尝试呈现 belongsToMany 关系数据。有问题的数据是一个帖子及其标签。我相信模型、表格设置正确。但我无法在刀片模板上呈现数据。如果我 var_dump 变量,我可以在输出中看到标签,但无法访问每个循环中的数据。
- 我设置了 3 个表:articles、tags、article_tag(pivot table)
Models:
tag.php
public function articles()
{
return $this->belongsToMany(Article::class);
}
Article.php
public function tags()
{
return $this->belongsToMany(Tag::class);
}
Controller:
ArticleController.php
public function show($id)
{
$articles = Article::with('tags', )->findorFail(1);
return view('/article', ['article' => $articles,]);
}
我要做的就是渲染文章和标签。如果运行 foreach 来获取标签,我会在刀片模板上遇到 foreach 错误。任何帮助将不胜感激。
object(App\Models\Article)#1016 (27) { ["casts":protected]=> array(2) { ["tags"]=> string(5) "array" ["categories"]=> string(5) "array" } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(8) "articles" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(9) { ["id"]=> int(1) ["created_at"]=> NULL ["updated_at"]=> NULL ["article_title"]=> string(10) "NewArticle" ["article_body"]=> string(13) "article text." ["author_id"]=> string(1) "1" ["article_status"]=> string(4) "live" ["likes_count"]=> string(1) "1" ["dislikes_count"]=> string(1) "2" } ["original":protected]=> array(9) { ["id"]=> int(1) ["created_at"]=> NULL ["updated_at"]=> NULL ["article_title"]=> string(10) "NewArticle" ["article_body"]=> string(13) "article text." ["author_id"]=> string(1) "1" ["article_status"]=> string(4) "live" ["likes_count"]=> string(1) "1" ["dislikes_count"]=> string(1) "2" } ["changes":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(1) { ["tags"]=> object(Illuminate\Database\Eloquent\Collection)#1229 (1) { ["items":protected]=> array(2) { [0]=> object(App\Models\Tag)#1234 (27) { ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(4) "tags" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(5) { ["id"]=> int(1) ["created_at"]=> NULL ["updated_at"]=> NULL ["tag_name"]=> string(4) "tag1" ["tag_status"]=> string(4) "live" } ["original":protected]=> array(7) { ["id"]=> int(1) ["created_at"]=> NULL ["updated_at"]=> NULL ["tag_name"]=> string(4) "tag1" ["tag_status"]=> string(4) "live" ["pivot_article_id"]=> string(1) "1" ["pivot_tag_id"]=> string(1) "1" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(1) { ["pivot"]=> object(Illuminate\Database\Eloquent\Relations\Pivot)#1227 (30) { ["incrementing"]=> bool(false) ["guarded":protected]=> array(0) { } ["connection":protected]=> NULL ["table":protected]=> string(11) "article_tag" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(2) { ["article_id"]=> string(1) "1" ["tag_id"]=> string(1) "1" } ["original":protected]=> array(2) { ["article_id"]=> string(1) "1" ["tag_id"]=> string(1) "1" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(false) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["pivotParent"]=> object(App\Models\Article)#1082 (27) { ["casts":protected]=> array(2) { ["tags"]=> string(5) "array" ["categories"]=> string(5) "array" } ["connection":protected]=> NULL ["table":protected]=> string(8) "articles" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(false) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(0) { } ["original":protected]=> array(0) { } ["changes":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } ["foreignKey":protected]=> string(10) "article_id" ["relatedKey":protected]=> string(6) "tag_id" } } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } [1]=> object(App\Models\Tag)#1231 (27) { ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(4) "tags" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(5) { ["id"]=> int(2) ["created_at"]=> NULL ["updated_at"]=> NULL ["tag_name"]=> string(4) "tag2" ["tag_status"]=> string(4) "live" } ["original":protected]=> array(7) { ["id"]=> int(2) ["created_at"]=> NULL ["updated_at"]=> NULL ["tag_name"]=> string(4) "tag2" ["tag_status"]=> string(4) "live" ["pivot_article_id"]=> string(1) "1" ["pivot_tag_id"]=> string(1) "2" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(1) { ["pivot"]=> object(Illuminate\Database\Eloquent\Relations\Pivot)#1226 (30) { ["incrementing"]=> bool(false) ["guarded":protected]=> array(0) { } ["connection":protected]=> NULL ["table":protected]=> string(11) "article_tag" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(2) { ["article_id"]=> string(1) "1" ["tag_id"]=> string(1) "2" } ["original":protected]=> array(2) { ["article_id"]=> string(1) "1" ["tag_id"]=> string(1) "2" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(false) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["pivotParent"]=> object(App\Models\Article)#1082 (27) { ["casts":protected]=> array(2) { ["tags"]=> string(5) "array" ["categories"]=> string(5) "array" } ["connection":protected]=> NULL ["table":protected]=> string(8) "articles" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(false) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(0) { } ["original":protected]=> array(0) { } ["changes":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } ["foreignKey":protected]=> string(10) "article_id" ["relatedKey":protected]=> string(6) "tag_id" } } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } } } } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } }
【问题讨论】:
-
感谢您的评论。我已经在控制器中尝试过这个调用,它仍然会转储出一个包含标签的数组,但我不确定如何访问标签。我已经使用 foreach ($tags as $tag) {{$tag->article->tag_name}} endforeach (@ removed) 来调用标签,但仍然没有输出。这是正确的吗?
-
您能否转储 $articles 并将输出粘贴到您的问题中。 dd($articles)