亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 雄辯 - 熱切加載嵌套關系

Laravel 雄辯 - 熱切加載嵌套關系

PHP
慕姐8265434 2022-08-19 15:25:22
我有以下3個表格:職位評論標簽職位: class Post extends Eloquent    {        public function comments()        {            return $this->hasMany(Comment::class,'post_id','id');        }    }    ** Post data **    {       'id' : 1,       'title' : 'bla bla',       'created_at: 'some date'    }評論:    class Comment extends Eloquent    {        public function comments()        {            return $this->belongsTo(Post::class,'id');        }        public function tags()        {            return $this->hasMany(Tag::class,'id','tags_ids');        }    }** Comments data **    {      'id' : 322,      'active' : true      'post_id' : 1,      'created_at: 'some date',      'tags_ids' : [1,2,3]    }標簽:    class Tag extends Eloquent    {        public function tags()        {            return $this->belongsTo(Comment::class,'tags_ids');        }    }    ** Tags data **    {      {'id' : 1,      'description' : 'some description1'      },      {'id' : 2,      'description' : 'some description2'      },      {'id' : 3,      'description' : 'some description3'      }    }帖子表有許多注釋,注釋表有許多與之關聯的標記。如何使用預先加載將所有這些表組合在一起?像這樣:$post = Post::where('id',1)->with(['comments' => function($q) {   $q->with('tags');}])->first();但此查詢始終在標記關系中返回空響應。我做錯了什么?所需的結果如下所示:{   'id' : 1,   'title' : 'bla bla',   'created_at: 'some date',   'comments':[{                  'id' : 322,                  'active' : true                  'post_id' : 1,                  'created_at: 'some date',                  'tags_ids' : [1,2,3],                  'tags' : [                            {'id' : 1,'description' : 'some description1'},                            {'id' : 2,  'description' : 'some description2'},                            {'id' : 3,'description' : 'some description3'}                           ],              }             ]}您可以看到帖子其中包含評論,評論內部也包含標簽關系。附言:我在我的項目中使用'jenssegers/laravel-mongodb'包,我試圖在沒有任何原始表達的情況下做到這一點。謝謝。
查看完整描述

3 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

你錯誤地定義了你的關系。注釋模型中有 as 數組,但標簽需要多對多關系。要實現這一點,你必須定義一個新表:tags_idscomment-tag


comment-tag

    comment_id - unsined big integer

    tag_id - unsigned big integer

然后在模型中修改關系,如下所示:Commenttags


class Comment extends Model

{

    /**

     * The tags that belong to the comment.

     */

    public function tags()

    {

        return $this->belongsToMany('App\Tag');

    }

}

這種關系的反面是:


class Tag extends Model

{

    /**

     * The comments that belong to the tag.

     */

    public function comments()

    {

        return $this->belongsToMany('App\Comment');

    }

}

然后,對于預先加載嵌套關系,可以使用“dot”語法:


$post = Post::with(['comments', 'comments.tags'])->find(1);

有關詳細信息,請參閱 Laravel 文檔中的多對多關系。


查看完整回答
反對 回復 2022-08-19
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

您可以使用

$post = Post::where('id',1)->with(['comments.tags'])->first();

它將加載所有評論以及評論標簽

鏈接搜索中的引用鏈接 https://laravel.com/docs/6.x/eloquent-relationshipsNested Eager Loading


查看完整回答
反對 回復 2022-08-19
?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

更新:您必須修復數據庫架構


表結構如下所示:


- posts

    - id

    - title

    - ...


- comments

    - id

    - active

    - post_id 

    - created_at


- tags

    - id

    - description

    - comment_id 

    - created_at


// App\Post 

class Post extends Eloquent 

{


    public function comments()

    {

        return $this->hasMany(Comment::class,'post_id','id');

    }


}


// App\Comment 

class Comment extends Eloquent 

{


    public function post()

    {

        return $this->belongsTo(Post::class);

    }


    public function tags()

    {

        return $this->hasMany(Tag::class);

    }


}



//App\Tag    

class Tag extends Eloquent 

{


    public function comment()

    {

        return $this->belongsTo(Comment::class);

    }


}





//...

$post = Post::with(['comments', 'comments.tags'])->find(1);

//...


查看完整回答
反對 回復 2022-08-19
  • 3 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號