简体   繁体   English

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

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

I'm creating a blog using Laravel 5 but I'm having a strange issue with a many-to-many (via pivot table) relationship. 我正在使用Laravel 5创建博客,但是我遇到了一个多对多(通过数据透视表)关系的奇怪问题。 My DB setup is the following: 我的数据库设置如下:

posts table: 帖子表:
id | id | bigint | bigint | 20 | 20 | PK | PK | Not Null | 不为空| Auto Increment 自动递增
title | 标题| varchar | varchar | 255 | 255 | Not Null 不为空
body | 身体| text | 文字| Not Null 不为空
category_id | category_id | bigint | bigint | 20 | 20 | Not Null 不为空
created_at | created_at | timestamp | 时间戳| Not Null 不为空
updated_at | Updated_at | timestamp | 时间戳| Not Null 不为空
published | 发表| boolean | 布尔| Not Null 不为空
deleted_at | delete_at | timestamp | 时间戳| Not Null 不为空

tags table: 标签表:
id | id | bigint | bigint | 20 | 20 | PK | PK | Not Null | 不为空| Auto Increment 自动递增
name | 名称| varchar | varchar | 255 | 255 | Not Null 不为空
created_at | created_at | timestamp | 时间戳| Not Null 不为空
updated_at | Updated_at | timestamp | 时间戳| Not Null 不为空

post_tag pivot table: post_tag数据透视表:
id | id | bigint | bigint | 20 | 20 | PK | PK | Not Null | 不为空| Auto Increment 自动递增
post_id | post_id | bigint | bigint | 20 | 20 | Not Null 不为空
tag_id | tag_id | bigint | bigint | 20 | 20 | Not Null 不为空
created_at | created_at | timestamp | 时间戳| Not Null 不为空
updated_at | Updated_at | timestamp | 时间戳| Not Null 不为空

Following are extracted from my models: 以下是我的模型的摘录:
Post 发布

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

Tag 标签

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

Now on the view if I do this: 现在在视图上,如果我这样做:

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

I'm returned the correct tag associated to the post, but obviously the post can have more tags so what I'm trying to do is a for each loop on $post->tags . 我返回了与该帖子相关的正确标签,但是显然该帖子可以包含更多标签,所以我想做的是$ post-> tags的每个循环一个。
Anyway if I try to do it, it seems the result is not usable and the loop never start. 无论如何,如果我尝试执行此操作,则结果似乎无法使用,并且循环永远不会开始。
Then I've tried to look at what I'm returned doing this: 然后,我尝试查看执行此操作后返回的内容:

var_dump($post->tags);

And I'm returned the following: 我返回了以下内容:

object(Illuminate\\Database\\Eloquent\\Collection)#199 (1) { ["items":protected]=> array(1) { [0]=> object(App\\Tag)#200 (21) { ["table":protected]=> string(4) "tags" ["fillable":protected]=> array(1) { [0]=> string(4) "name" } ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(4) { ["id"]=> string(1) "2" ["name"]=> string(3) "Tag" ["created_at"]=> string(19) "2015-02-25 15:26:29" ["updated_at"]=> string(19) "2015-02-25 15:26:29" } ["original":protected]=> array(6) { ["id"]=> string(1) "2" ["name"]=> 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"]=> string(1) "6" ["pivot_tag_id"]=> string(1) "2" } ["relations":protected]=> array(1) { ["pivot"]=> object(Illuminate\\Database\\Eloquent\\Relations\\Pivot)#193 (24) { ["parent":protected]=> object(App\\Post)#198 (22) { ["t 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 able":protected]=> string(5) "posts" ["dates":protected]=> array(1) { [0]=> string(10) "deleted_at" } ["fillable":protected]=> array(4) { [0]=> string(5) "title" [1]=> string(4) "body" [2]=> string(11) "category_id" [3]=> string(9) "published" } ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(8) { ["id"]=> string(1) "6" ["title"]=> string(4) "Test" ["body"]=> string(24) " Test 能力”:受保护] =>字符串(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"]=> string(1) "1" ["created_at"]=> string(19) "2015-02-25 15:49:20" ["updated_at"]=> string(19) "2015-02-25 15:49:20" ["published"]=> string(1) "1" ["deleted_at"]=> NULL } ["original":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"]=> string(1) "1" ["created_at"]=> string(19) "2015-02-25 15:49:20" ["updated_at"]=> string(19) "2015-02-25 15:49:20" ["published"]=> string(1) "1" ["deleted_at"]=> NULL } ["relations":protected]=> array(2) { ["likes"]=> object(Illuminate\\Database\\Eloquent\\Collection)#197 (1) { ["items":protected]=> array(0) { } } ["tags"]=> RECURSION } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) " " } ["casts":protected]=> array(0) { } ["touches":protected]=> 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) { } ["connection":protected]=> NULL ["table":protected]=> string(8) "post_tag" ["primaryKey":protected]=> s “ [[” 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"]=> string(1) "6" ["tag_id"]=> string(1) "2" } ["original":protected]=> array(2) { ["post_id"]=> string(1) "6" ["tag_id"]=> string(1) "2" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) } } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) " " } ["dates":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["ob 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) } } } servables“:protected] => array(0){} [” with“:protected] => array(0){} [” morphClass“:protected] => NULL [” exists“] => bool(true)} }}

I've highlighted the part that for me is giving me the problem, for some reason it seems it is having some kind of query recursion which I can't understand why it happens. 我已经强调了对我来说给我带来问题的那部分,由于某种原因,它似乎具有某种查询递归,但我不明白为什么会发生。
Any Idea? 任何想法?

Thanks, 谢谢,
Matteo 马泰奥

You are getting the query collection. 您正在获取查询集合。 You may convert collection to array using ->toArray() so you can get only the needed data: 您可以使用->toArray()将集合转换为数组,以便仅获取所需的数据:

$post->tags->toArray();

Maybe your autoload_classmap.php wasn't up to date. 也许您的autoload_classmap.php不是最新的。

This file is regenerated on each dump-autoload. 该文件在每个转储自动加载时重新生成。 If you have a new class somewhere in your project it will not be loaded unless it is included in autoload_classmap 如果您的项目中某处有新类,除非将其包含在autoload_classmap中,否则将不会加载该类

composer dump-autoload

This may happened to you, maybe automatically by installing or updating package or something. 这可能发生在您身上,可能是通过安装或更新软件包或其他东西自动发生的。

Good tip: If you face a problem in Laravel, try to run composer dump-autoload , which often may fix some problems. 提示:如果您在Laravel中遇到问题,请尝试运行composer dump-autoload ,这通常可以解决一些问题。

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

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