簡體   English   中英

Laravel 5雄辯的多對多關系問題

[英]Laravel 5 eloquent many-to-many relationship issue

我正在使用Laravel 5創建博客,但是我遇到了一個多對多(通過數據透視表)關系的奇怪問題。 我的數據庫設置如下:

帖子表:
id | bigint | 20 | PK | 不為空| 自動遞增
標題| varchar | 255 | 不為空
身體| 文字| 不為空
category_id | bigint | 20 | 不為空
created_at | 時間戳| 不為空
Updated_at | 時間戳| 不為空
發表| 布爾| 不為空
delete_at | 時間戳| 不為空

標簽表:
id | bigint | 20 | PK | 不為空| 自動遞增
名稱| varchar | 255 | 不為空
created_at | 時間戳| 不為空
Updated_at | 時間戳| 不為空

post_tag數據透視表:
id | bigint | 20 | PK | 不為空| 自動遞增
post_id | bigint | 20 | 不為空
tag_id | bigint | 20 | 不為空
created_at | 時間戳| 不為空
Updated_at | 時間戳| 不為空

以下是我的模型的摘錄:
發布

public function tags(){
    return $this->belongsToMany('App\Tag');
}

標簽

public function posts(){
    return $this->belongsToMany('App\Post');
}

現在在視圖上,如果我這樣做:

var_dump($post->tags->first()->name);

我返回了與該帖子相關的正確標簽,但是顯然該帖子可以包含更多標簽,所以我想做的是$ post-> tags的每個循環一個。
無論如何,如果我嘗試執行此操作,則結果似乎無法使用,並且循環永遠不會開始。
然后,我嘗試查看執行此操作后返回的內容:

var_dump($post->tags);

我返回了以下內容:

object(Illuminate \\ Database \\ Eloquent \\ Collection)#199(1){[“” items“:protected] => array(1){[0] => object(App \\ Tag)#200(21){[” table “:protected] => string(4)”標簽“ [” fillable“:protected] => array(1){[0] => string(4)” name“} [” connection“:protected] => NULL [“ primaryKey”:受保護] =>字符串(2)“ id” [“ perPage”:受保護] => int(15)[“ incrementing”] => bool(true)[“ timestamps”] => bool(true )[“屬性”:受保護] => array(4){[“ id”] =>字符串(1)“ 2” [“名稱”] =>字符串(3)“標記” [“ created_at”] => string(19)“ 2015-02-25 15:26:29” [“ updated_at”] => string(19)“ 2015-02-25 15:26:29”} [“原始”:受保護的] =>數組(6){[“ id”] =>字符串(1)“ 2” [“ name”] =>字符串(3)“ Tag” [“ created_at”] =>字符串(19)“ 2015-02-25 15 :26:29“ [” updated_at“] =>字符串(19)” 2015-02-25 15:26:29“ [” pivot_post_id“] =>字符串(1)” 6“ [” pivot_tag_id“] =>字符串(1)“ 2”} [“關系”:受保護] =>數組(1){[“”軸心“] =>對象(Illuminate \\ Database \\ Eloquent \\ Relations \\ Pivot)#193(24){[”父母“ :protected] => object(App \\ Post)#198(22){[“ t 能力”:受保護] =>字符串(5)“帖子” [“日期”:受保護] =>數組(1){[0] =>字符串(10)“ deleted_at”} [“可填充”:受保護] => array(4){[0] =>字符串(5)“標題” [1] =>字符串(4)“ body” [2] =>字符串(11)“ category_id” [3] =>字符串(9) “已發布”} [“連接”:受保護] => NULL [“ primaryKey”:受保護] =>字符串(2)“ id” [“ perPage”:受保護] => int(15)[“遞增”] => bool(true)[“ timestamps”] => bool(true)[“ attributes”:protected] => array(8){[“ id”] => string(1)“ 6” [“ title”] => string(4)“ Test” [“ body”] => string(24)“ Test

“ [[” category_id“] =>字符串(1)” 1“ [” created_at“] =>字符串(19)” 2015-02-25 15:49:20“ [” updated_at“] =>字符串(19)” 2015-02-25 15:49:20“ [”已發布“] => string(1)” 1“ [” deleted_at“] => NULL} [”原始“:受保護的] => array(8){[” id“] => string(1)” 6“ [” title“] => string(4)” Test“ [” body“] => string(24)” Test

“ [[” category_id“] =>字符串(1)” 1“ [” created_at“] =>字符串(19)” 2015-02-25 15:49:20“ [” updated_at“] =>字符串(19)” 2015-02-25 15:49:20“ [” published“] => string(1)” 1“ [” deleted_at“] => NULL} [” relations“:protected] => array(2){[”點贊“] =>對象(Illuminate \\ Database \\ Eloquent \\ Collection)#197(1){[”項目“:受保護] => array(0){}} [”標簽“] => 恢復 } [”隱藏“ :protected] => array(0){} [“可見”:受保護] => array(0){} [“ appends”:protected] => array(0){} [“ guarded”:protected] => array(1){[0] =>字符串(1)“ ”} [“ casts”:受保護] => array(0){} [“ touches”:受保護] => array(0){} [“ observables “:protected] => array(0){} [” with“:protected] => array(0){} [” morphClass“:protected] => NULL [” exists“] => bool(true)[” forceDeleting“:protected] => bool(false)} [” foreignKey“:protected] => string(7)” post_id“ [” otherKey“:protected] => string(6)” tag_id“ [” guarded“:protected ] => array(0){} [“連接”:受保護] => NULL [“表”:受保護] =>字符串(8)“ post_tag” [“ primaryKey”:受保護] => s tring(2)“ id” [“ perPage”:protected] => int(15)[“ incrementing”] => bool(true)[“ timestamps”] => bool(false)[“ attributes”:protected] = > array(2){[“” post_id“] =>字符串(1)” 6“ [” tag_id“] =>字符串(1)” 2“} [”原始“:受保護的] => array(2){[ “ post_id”] =>字符串(1)“ 6” [“ tag_id”] =>字符串(1)“ 2”} [“關系”:受保護的] => array(0){} [“隱藏”:受保護的] => array(0){} [“可見”:受保護] => array(0){} [“ appends”:受保護] => array(0){} [“ fillable”:protected] => array(0 ){} [“日期”:受保護] => array(0){} [“ casts”:受保護] => array(0){} [“ touches”:受保護] => array(0){} [“ observables“:protected] => array(0){} [” with“:protected] => array(0){} [” morphClass“:protected] => NULL [” exists“] => bool(true)} } [“隱藏”:受保護] => array(0){} [“可見”:受保護] => array(0){} [“追加”:受保護] => array(0){} [“受保護” :protected] => array(1){[0] => string(1)“ ”} [“ dates”:protected] => array(0){} [“ casts”:protected] => array(0) {} [“ touches”:受保護] => array(0){} [“ ob servables“:protected] => array(0){} [” with“:protected] => array(0){} [” morphClass“:protected] => NULL [” exists“] => bool(true)} }}

我已經強調了對我來說給我帶來問題的那部分,由於某種原因,它似乎具有某種查詢遞歸,但我不明白為什么會發生。
任何想法?

謝謝,
馬泰奧

您正在獲取查詢集合。 您可以使用->toArray()將集合轉換為數組,以便僅獲取所需的數據:

$post->tags->toArray();

也許您的autoload_classmap.php不是最新的。

該文件在每個轉儲自動加載時重新生成。 如果您的項目中某處有新類,除非將其包含在autoload_classmap中,否則將不會加載該類

composer dump-autoload

這可能發生在您身上,可能是通過安裝或更新軟件包或其他東西自動發生的。

提示:如果您在Laravel中遇到問題,請嘗試運行composer dump-autoload ,這通常可以解決一些問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM