【发布时间】:2014-01-03 20:02:30
【问题描述】:
我开始学习 laravel 4 但我还很远,我已经看过 ORM 和关系所以我有以下问题:
我有 3 种类型的表:users、posts、content_post,我希望得到与用户有关的所有帖子和内容的结果。
用户表
id | userame | name |
1 Ellie Elen
2 Pol Paul
帖子表
id | id_poster |
1 1
2 1
3 2
content_post
id | id_post | text | link | video | photo
1 1 hey NULL NULL tree.png
2 2 woo http NULL NULL
3 3 Hi NULL NULL NULL
php
class User Extends Eloquent {
function posts() {
return $this->belongsToMany('posts','id_poster');
}
}
class Posts Extends Eloquent {
function user() {
return $this->HasOne('users');
}
function content() {
return $this->HasOne('content_post','id_post');
}
}
class Content Extends Eloquent {
function posts() {
return $this->HasOne('posts');
}
}
?>
然后我会去得到这样的结果,但它什么也没显示
$user = new User;
$user = $user->find(1)->posts();
我哪里出错了?
【问题讨论】: