繁体   English   中英

Laravel 8 渲染belongsToMany关系

[英]Laravel 8 rendering belongsToMany relationship

我正在尝试呈现 belongsToMany 关系数据。 有问题的数据是一个帖子及其标签。我相信模型、表格设置正确。 但我无法在刀片模板上呈现数据。 如果我 var_dump 变量,我可以看到 output 中的标签,但无法访问每个循环中的数据。

  • 我设置了 3 个表:文章、标签、article_tag(数据透视表)
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) "*" } }

你正在做一个多对多的关系,因此你必须使用这样的东西:

tag.php
   public function articles()
    {
      return$this->morphedByMany(Article::class, 'article_tag');
    } 
Article.php
    public function tags()
    {
      return $this->morphedByMany(Tag::class, 'article_tag');
    } 

文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM