Laravel Eloquent 嵌套关系只返回第一个元素的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22231157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent nested relations returns data only on the first element
提问by R. Gulbrandsen
Intro
介绍
Im havning some trubble getting the data on all the related elements. Im using Laravel as a REST backend service, exposing Json to the front-end javascript application.
我在获取所有相关元素的数据时遇到了一些麻烦。我使用 Laravel 作为 REST 后端服务,将 Json 暴露给前端 javascript 应用程序。
Data structure
数据结构
Consider I have the following tables:
考虑我有以下表格:
+----------------+ +----------------+ +-------------+
|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 |
+----------------+ +----------------+
A topic has 0 to many posts, and it has an author (user)
一个主题有 0 到多个帖子,它有一个作者(用户)
A post has one author (user)
一个帖子有一个作者(用户)
UML: http://i58.servimg.com/u/f58/11/26/57/95/intrep10.png
UML:http: //i58.servimg.com/u/f58/11/26/57/95/intrep10.png
Laravel Models
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');
}
}
Controller
控制器
return Topic::where('id', '=', $topicId)
->with('author', 'posts.author')
->get();
Output
输出
[{
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,
}]
}]
Question
题
As you can see of the jsoncode, both the posts are created by the same user (with id 1), but only the first has the author object on it. Any pointers on how to figure out my problem would be perfect.
正如您在 jsoncode 中看到的,两个帖子都是由同一个用户(ID 为 1)创建的,但只有第一个拥有作者对象。关于如何解决我的问题的任何指示都是完美的。
Disclaimer
免责声明
This is a striped down version of my project, as I dont want to spam the question with information. If I lack serten elements to the question, I will be happy to supply it.
这是我的项目的精简版,因为我不想用信息向问题发送垃圾邮件。如果我对这个问题缺乏足够的元素,我很乐意提供它。
Solution
解决方案
My model mapping was off.
我的模型映射关闭了。
public function author() {
return $this->belongsTo('User', 'user_id', 'id');
}
makes sure that its the user_id its looking for in the posts table, up against the id column in the users table
确保它的 user_id 在帖子表中查找,与用户表中的 id 列相对
SELECT * FROM users WHERE id = posts.user_id;
采纳答案by The Alpha
In your Topic
model the relationship for the author
should be
在你的Topic
模型中的关系author
应该是
class Topic extends Eloquent {
//...
public function author() {
return $this->belongsTo('User');
}
}
Same goes to your Post
model:
同样适用于您的Post
模型:
class Post extends Eloquent {
// ...
public function author() {
return $this->belongsTo('User');
}
}
It's because, in both tables topics
and posts
you have user_id
and the user_id
relates to the users
table, so, think this way, each user_id
in your topics
and posts
table belongs to user
table where the corresponding field is id
in the users
table. In this case, both topics
and posts
tables are child of users
table.
这是因为,这两个表中topics
和posts
你有user_id
和user_id
涉及users
表,所以,这样的想法,每个user_id
在你topics
和posts
表属于user
表,其中对应的字段是id
在users
表中。在这种情况下,topics
和posts
表都是表的子项users
。
回答by Lucky Soni
I think you are mixing your relationships. Should it not be:
我认为你正在混合你的关系。不应该是:
A Post
belongs to a User
APost
属于User
A Post
belongs to a Topic
APost
属于Topic
A User
has many Post
AUser
有很多Post
A Topic
has many Post
ATopic
有很多Post
Table Fields
表格字段
Users: id, name
用户:id、姓名
Posts: id, user_id, topic_id, content
帖子:id、user_id、topic_id、内容
Topic: id, title, content
主题:id、标题、内容
Models
楷模
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');
}
}
To get posts with author and topic:
要获取带有作者和主题的帖子:
return Post::where('topic_id', '>=', $topicId)
->with('topic', 'author')
->get();