【问题标题】:Laravel 5 eloquent many-to-many relationship issueLaravel 5 雄辩的多对多关系问题
【发布时间】:2015-04-28 14:56:42
【问题描述】:

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

帖子表:
编号 |大整数 | 20 | PK |不为空 |自动增量
标题 |变量 | 255 |不为空
身体 |正文 |不为空
类别 ID |大整数 | 20 |不为空
created_at |时间戳 |不为空
更新时间 |时间戳 |不为空
发表 |布尔值 |不为空
删除_at |时间戳 |不为空

标签表:
编号 |大整数 | 20 | PK |不为空 |自动增量
姓名 |变量 | 255 |不为空
created_at |时间戳 |不为空
更新时间 |时间戳 |不为空

post_tag 数据透视表:
编号 |大整数 | 20 | PK |不为空 |自动增量
post_id |大整数 | 20 |不为空
tag_id |大整数 | 20 |不为空
created_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 上的 for each 循环em>
无论如何,如果我尝试这样做,似乎结果不可用并且循环永远不会开始。
然后我试着看看我这样做返回了什么:

var_dump($post->tags);

我收到了以下信息:

object(Illuminate\Database\Eloquent\Collection)#199 (1) { ["items":protected]=> 数组(1) { [0]=> 对象(App\Tag)#200 (21) { ["table":protected]=> 字符串(4) "tags" ["fillable":protected]=> 数组(1){[0]=>字符串(4)“名称”}[“连接”:受保护]=> NULL ["primaryKey":protected]=> 字符串(2) "id" ["perPage":protected]=> int(15) ["递增"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(4) { ["id"]=> string(1) "2" ["name"]=> string(3) "标签" ["created_at"]=> string(19) "2015-02-25 15:26:29" ["updated_at"]=> 字符串(19) "2015-02-25 15:26:29" } ["original":protected]=> array(6) { ["id"]=> string(1) "2" ["name"]=> 字符串(3)“标签”[“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) { ["parent":protected]=> 对象(App\Post)#198 (22) { ["table":protected]=> 字符串(5) "posts" ["dates":protected]=> 数组(1) { [0]=> string(10) "deleted_at" } ["fillable":protected]=> array(4) { [0]=>字符串(5)“标题”[1]=>字符串(4)“正文”[2]=>字符串(11) "category_id" [3]=> string(9) "已发布" } ["connection":protected]=> NULL ["primaryKey":protected]=> 字符串(2) "id" ["perPage":protected]=> int(15) ["递增"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(8) { ["id"]=> string(1) "6" ["title"]=> string(4) "测试" ["body"]=> string(24) "测试

" ["category_id"]=> 字符串(1) "1" ["created_at"]=> 字符串(19) "2015-02-25 15:49:20" ["updated_at"]=> 字符串(19) "2015-02-25 15:49:20" ["已发布"]=> 字符串 (1) "1" ["deleted_at"]=> NULL } ["original":protected]=> array(8) { ["id"]=> string(1) "6" ["title"]=> 字符串(4)“测试”[“正文”]=>字符串(24)“测试”

" ["category_id"]=> 字符串(1) "1" ["created_at"]=> 字符串(19) "2015-02-25 15:49:20" ["updated_at"]=> 字符串(19) "2015-02-25 15:49:20" ["已发布"]=> 字符串 (1) "1" ["deleted_at"]=> NULL } [“关系”:受保护]=> 数组(2){ [“喜欢”]=> 对象(Illuminate\Database\Eloquent\Collection)#197 (1) { ["items":protected]=> array(0) { } } ["tags"]=> RECURSION } [“隐藏”:受保护]=> 数组(0){ } [“可见”:受保护]=> 数组(0){ } ["appends":protected]=> 数组(0) { } ["guarded":protected]=> 数组(1) { [0]=> 字符串(1) "" } ["casts":protected]=> 数组(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> 数组(0) { } ["with":protected]=> 数组(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["forceDeleting":protected]=> bool(false) } ["foreignKey":protected]=> string(7) "post_id" ["otherKey":protected]=> string(6) "tag_id" [“受保护”:受保护]=> 数组(0){ } [“连接”:受保护]=> NULL ["table":protected]=> 字符串(8) "post_tag" ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(false) ["attributes":protected]=> 数组(2){[“post_id”]=>字符串(1)“6”[“tag_id”]=>字符串(1)“2”} ["original":protected]=> array(2) { ["post_id"]=> string(1) "6" ["tag_id"]=> string(1) "2" } ["relations":protected]=> array(0) { } [“隐藏”:受保护]=> 数组(0){ } [“可见”:受保护]=> 数组(0){ } ["appends":protected]=> 数组(0) { } ["fillable":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["casts":protected]=> 数组(0){}[“触摸”:受保护]=>数组(0){} ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) } } [“隐藏”:受保护]=> 数组(0){ } [“可见”:受保护]=> 数组(0){ } ["appends":protected]=> 数组(0) { } ["guarded":protected]=> 数组(1) { [0]=> 字符串(1) "" } ["日期":protected]=> 数组(0) { } ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) } } }

我已经强调了对我来说给我带来问题的部分,由于某种原因,它似乎有某种查询递归,我无法理解它为什么会发生。
任何想法?

谢谢,
马特奥

【问题讨论】:

  • 不知道有没有用,反正我的MySQL版本是5.5.41
  • 不要问我为什么,但现在突然开始工作,我没有对代码做任何更改,我无法理解发生了什么。我希望我最终能够复制它,以便与大家分享。很抱歉浪费您的时间

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


【解决方案1】:

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

此文件在每次转储自动加载时重新生成。如果你的项目中有一个新类,除非它包含在 autoload_classmap 中,否则它不会被加载

composer dump-autoload

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

提示:如果您在 Laravel 中遇到问题,请尝试运行 composer dump-autoload,这通常可以解决一些问题。

【讨论】:

    【解决方案2】:

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

    $post->tags->toArray();
    

    【讨论】:

    • 这并不完全有效,因为我正在返回标签表上的第一个标签,而不是实际上分配给帖子的标签。我还希望标签类能够执行以下操作: $tag->name
    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 2016-11-03
    • 2013-01-22
    • 1970-01-01
    • 2012-10-08
    • 2018-10-27
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多