【问题标题】:Laravel Eloquent nested relations returns data only on the first elementLaravel Eloquent 嵌套关系仅返回第一个元素的数据
【发布时间】:2014-04-09 11:35:09
【问题描述】:

简介

我在获取所有相关元素的数据时遇到了一些麻烦。我使用 Laravel 作为 REST 后端服务,将 Json 暴露给前端 javascript 应用程序。

数据结构

考虑一下我有以下表格:

+----------------+ +----------------+ +-------------+
|topics          | |posts           | |users        |
+----------------+ +----------------+ +-------------+
|id: int         | |id: int         | |id: int      |
|title: varchar  | |content: varchar| |name: varchar|
|content: varchar| |user_id: int    | +-------------+
|user_id: int    | |topic_id: int   |
+----------------+ +----------------+ 

一个主题有 0 到多个帖子,并且它有一个作者(用户)

一篇文章有​​一个作者(用户)

UML:http://i58.servimg.com/u/f58/11/26/57/95/intrep10.png

Laravel 模型

class User extends Eloquent {
    protected $table = 'users';

    public function topics() {
        reutrn $this->hasMany('Topic');
    }

    public function posts() {
        reutrn $this->hasMany('Post');
    }
}

class Topic extends Eloquent {
    protected $table = 'topics';

    public function posts() {
            return $this->hasMany('Post');
    }

    public function author() {
         return $this->hasOne('User', 'id');
    }
}

class Post extends Eloquent {
    protected $table = 'posts';

    public function topic() {
        return $this->belongsTo('Topic');
    }

    public function author() {
        return $this->hasOne('User', 'id');
    }
}

控制器

return Topic::where('id', '=', $topicId)
    ->with('author', 'posts.author')
    ->get();

输出

[{
    id: 1,
    title: "My Topic",
    content: "With opinions about the darkside",
    user_id: 1,
    created_at: "2014-03-06",
    updated_at: "2014-03-06",
    author: {
        id: 1,
        name: "JamesBond",
        created_at: "2014-03-06",
        updated_at: "2014-03-06",
    },
    posts: [{
        id: 1,
        content: "Reply 1 on topic 1",
        user_id: 1,
        created_at: "2014-03-06",
        updated_at: "2014-03-06",
        author: {
            id: 1,
            name: "JamesBond",
            created_at: "2014-03-06",
            updated_at: "2014-03-06",
        },
    },
    {
        id: 2,
        content: "Reply 2 on topic 1",
        user_id: 1,
        created_at: "2014-03-06",
        updated_at: "2014-03-06",
        author: null,
    }]
}]

问题

正如您在 jsoncode 中看到的,两个帖子都是由同一个用户(ID 为 1)创建的,但只有第一个帖子上有作者对象。 任何关于如何找出我的问题的指示都是完美的。

免责声明

这是我项目的精简版,因为我不想用信息向问题发送垃圾邮件。如果我的问题缺少明确的元素,我很乐意提供。

解决方案

我的模型映射已关闭。

public function author() {
    return $this->belongsTo('User', 'user_id', 'id');
}

确保它在帖子表中查找的 user_id 与用户表中的 id 列相对应

SELECT * FROM users WHERE id = posts.user_id;

【问题讨论】:

    标签: php laravel entity-relationship eloquent eager-loading


    【解决方案1】:

    我认为你正在混合你的人际关系。不应该是:

    Post 属于 User

    Post 属于 Topic

    一个User 有很多Post

    一个Topic 有很多Post

    表格字段

    用户:id、姓名

    帖子:id、user_id、topic_id、内容

    主题:id、标题、内容

    模型

    class User extends Eloquent {
        protected $table = 'users';
    
        public function posts() {
            reutrn $this->hasMany('Post');
        }
    }
    
    class Topic extends Eloquent {
        protected $table = 'topics';
    
        public function posts() {
            return $this->hasMany('Post');
        }
    
    }
    
    class Post extends Eloquent {
        protected $table = 'posts';
    
        public function topic() {
            return $this->belongsTo('Topic');
        }
    
        public function author() {
            return $this->belongsTo('User', 'id');
        }
    }
    

    要获取带有作者和主题的帖子:

    return Post::where('topic_id', '>=', $topicId)
        ->with('topic', 'author')
        ->get();
    

    【讨论】:

    • 你说的对,帖子和主题之间的联系是缺失的,但我现在会添加它
    • 我添加了一个 UML 图,使其更清晰。不要以为我混合了关系。一个话题有标题有内容,有人写,有人评论,有内容
    【解决方案2】:

    在您的 Topic 模型中,author 的关系应该是

    class Topic extends Eloquent {
    
        //...
    
        public function author() {
            return $this->belongsTo('User');
        }
    }
    

    同样适用于您的Post 模型:

    class Post extends Eloquent {
    
        // ...
    
        public function author() {
            return $this->belongsTo('User');
        }
    }
    

    这是因为,在 topicsposts 两个表中,您都有 user_id 并且 user_idusers 表相关,所以,这样想,每个 user_id 在您的 topicsposts表属于user表,对应的字段是users表中的id。在这种情况下,topicsposts 表都是 users 表的子表。

    【讨论】:

    • 从“hasOne”切换到“belongsTo”返回相同的结果。 id 为 2 的帖子仍然有 author:null 和 user_id:1
    • 确保您的两个帖子都包含相应的user_id,并且只尝试一次->with('posts.author'),仅用于调试。另外,尝试反过来,User::whereId(1)->with('posts')->get() 并检查您收到了多少帖子。
    • user_id 位于数据库的两行 :: 中,只有 'posts.author' 它返回如图所示的帖子,但主题上没有作者 :: 行返回带有帖子的用户:[.. ] 两个帖子都正确 :: 感谢您的时间
    • 帖子中仍然没有作者(2)
    • 你的问题让我开始思考。 Post::whereId(1)->with('author')->get(); 返回作者。 Post::whereId(2)->with('author')->get(); 返回 null,我们正在做某事
    猜你喜欢
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多