Laravel 5雄辩的多对多关系问题

时间:2015-02-26 09:41:52

标签: mysql laravel many-to-many eloquent relationship

我正在使用Laravel 5创建一个博客,但我遇到了一个多对多(通过数据透视表)关系的奇怪问题。 我的数据库设置如下:

帖子表:
id | bigint | 20 | PK |不是空的自动增量
标题| varchar | 255 |不是空的 身体|文字|不是空的 category_id | bigint | 20 |不是空的 created_at |时间戳|不是空的 updated_at |时间戳|不是空的 出版|布尔值|不是空的 deleted_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-上的每个循环>标签
无论如何,如果我尝试这样做,似乎结果是不可用的,循环永远不会开始 然后我试着看看我回来做了什么:

var_dump($post->tags);

我回复了以下内容:

  

object(Illuminate \ Database \ Eloquent \ Collection)#199(1){   ["项目":保护] => array(1){[0] => object(App \ Tag)#200(21){   ["表":保护] => string(4)" tags" ["可填充":保护] =>   array(1){[0] => string(4)" name" } [" connection":protected] =>空值   ["的PrimaryKey":保护] => string(2)" id" [" perPage":保护] =>   int(15)["递增"] => bool(true)[" timestamps"] =>布尔(真)   ["属性":保护] => array(4){[" id"] =>字符串(1)" 2"   ["名称"] => string(3)" Tag" [" created_at"] => string(19)" 2015-02-25   15:26:29" ["的updated_at"] => string(19)" 2015-02-25 15:26:29" }   ["原":保护] => array(6){[" id"] =>字符串(1)" 2" ["名称"] =>   string(3)" Tag" [" created_at"] => string(19)" 2015-02-25 15:26:29"   ["的updated_at"] => string(19)" 2015-02-25 15:26:29" [" pivot_post_id"] =>   字符串(1)" 6" [" pivot_tag_id"] =>字符串(1)" 2" }   ["关系":保护] => array(1){[" pivot"] =>   对象(Illuminate \ Database \ Eloquent \ Relations \ Pivot)#193(24){   ["父":保护] =>对象(App \ Post)#198(22){   ["表":保护] =>字符串(5)"帖子" ["日期":保护] =>阵列(1)   {[0] => string(10)" deleted_at" } [" fillable":protected] => array(4){   [0] => string(5)" title" [1] => string(4)" body" [2] =>串(11)   " CATEGORY_ID" [3] =>字符串(9)"已发布" } [" connection":protected] =>   NULL [" primaryKey":protected] => string(2)" id" [" perPage":保护] =>   int(15)["递增"] => bool(true)[" timestamps"] =>布尔(真)   ["属性":保护] => array(8){[" id"] =>字符串(1)" 6"   ["标题"] => string(4)" Test" ["主体"] =>字符串(24)"测试

     

" [" CATEGORY_ID"] =>字符串(1)" 1" [" created_at"] =>串(19)   " 2015-02-25 15:49:20" ["的updated_at"] => string(19)" 2015-02-25   15:49:20" ["发表"] =>字符串(1)" 1" [" deleted_at"] =>空值 }   ["原":保护] => array(8){[" id"] =>字符串(1)" 6" ["标题"] =>   string(4)" Test" ["主体"] =>字符串(24)"测试

     

" [" CATEGORY_ID"] =>字符串(1)" 1" [" created_at"] =>串(19)   " 2015-02-25 15:49:20" ["的updated_at"] => string(19)" 2015-02-25   15:49:20" ["发表"] =>字符串(1)" 1" [" deleted_at"] =>空值 }   ["关系":保护] => array(2){[" likes"] =>   对象(Illuminate \ Database \ Eloquent \ Collection)#197(1){   ["项目":保护] => array(0){}} [" tags"] => RECURSION }   ["隐藏":保护] => array(0){} [" visible":protected] => array(0){   } ["追加":protected] => array(0){} [" guarded":protected] =>   array(1){[0] => string(1)" " } ["演员":受保护] =>数组(0){}   ["触摸":保护] => array(0){} [" observables":protected] =>   array(0){} [" with":protected] =>数组(0){}   [" morphClass":保护] => NULL ["存在"] =>布尔(真)   [" forceDeleting":保护] => bool(false)} [" foreignKey":protected] =>   string(7)" post_id" [" otherKey":保护] => string(6)" tag_id"   ["防护":保护] => array(0){} [" connection":protected] =>空值   ["表":保护] => string(8)" post_tag" ["的PrimaryKey":保护] =>   string(2)" id" [" perPage":保护] => int(15)["递增"] =>   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" } [" relations":protected] =>数组(0){}   ["隐藏":保护] => array(0){} [" visible":protected] => array(0){   } ["追加":protected] => array(0){} [" fillable":protected] =>   array(0){} [" dates":protected] => array(0){} [" casts":protected] =>   array(0){} [" touches":protected] =>数组(0){}   ["观测量":保护] => array(0){} [" with":protected] =>阵列(0)   {}#" morphClass":protected] => NULL ["存在"] => bool(true)}}   ["隐藏":保护] => array(0){} [" visible":protected] => array(0){   } ["追加":protected] => array(0){} [" guarded":protected] =>   array(1){[0] => string(1)" " } ["日期":受保护] =>数组(0){}   ["投射":保护] => array(0){} [" touches":protected] => array(0){   } [" observables":protected] => array(0){} [" with":protected] =>   array(0){} [" morphClass":protected] => NULL ["存在"] =>布尔(真)}   }}

我突出强调了对我来说问题的部分,由于某种原因,似乎它正在进行某种查询递归,我无法理解为什么会发生这种情况。
任何的想法?

谢谢,
利玛

2 个答案:

答案 0 :(得分:1)

您正在获取查询集合。 您可以使用->toArray()将集合转换为数组,这样您就只能获得所需的数据:

$post->tags->toArray();

答案 1 :(得分:0)

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

在每个dump-autoload上重新生成此文件。如果项目中某处有新类,除非它包含在autoload_classmap中,否则不会加载它

composer dump-autoload

这可能发生在您身上,可能是通过安装或更新包或其他内容自动发生的。

好的提示:如果你在Laravel中遇到问题,请尝试运行作曲家dump-autoload,这通常可以解决一些问题。