Php Laravel-如何快速加载多对多关系
如何加载属于具有article user、comments和comments user的类别的文章 Laravel版本是4.2.17 使用查询事件侦听器,并将sql查询转储到页面。HomeController方法工作得很好,急切地加载所有文章而不存在N+1问题 但是,getCategory方法可以加载所有文章,但在foreach循环中执行sql查询以获取文章用户、注释和注释用户 --数据库表Php Laravel-如何快速加载多对多关系,php,laravel-4,Php,Laravel 4,如何加载属于具有article user、comments和comments user的类别的文章 Laravel版本是4.2.17 使用查询事件侦听器,并将sql查询转储到页面。HomeController方法工作得很好,急切地加载所有文章而不存在N+1问题 但是,getCategory方法可以加载所有文章,但在foreach循环中执行sql查询以获取文章用户、注释和注释用户 --数据库表 articles - id - user_id - body comment
articles
- id
- user_id
- body
comments
- id
- user_id
- article_id
- body
users
- id
- name
cat
- id
- name
article_cat
- id
- article_id
- cat_id
--模型
--控制器
class HomeController extends BaseController {
// Get all articles
public function home() {
$articles = Article::with(['user', 'cats', 'comments', 'comments.user'])->get();
}
}
class CategoryController extends BaseController {
public function getCategory($categoryid) {
// How do I eager load articles the belong to this category with the article user, comments and comments user
$category = Cat::with('articles')
->find($categoryid)
->articles()
->get();
}
}
编辑 已对控制器进行了一些更改,但仍不急于加载,下面是新控制器
public function getCategory($categoryid) {
$cat = Cat::where('id', $categoryid)->firstOrFail();
$category = Cat::with('articles')
->with('articles.user')
->with('articles.comments')
->find($categoryid)
->articles()
->orderBY('created_at', 'desc')
->take(Config::get('settings.num_posts'))
->get();
foreach ($category as $article) {
echo '<p> Article body = ' . $article->body . '</p>';
echo '<p> Article user = ' . $article->user->name . '</p>';
echo '<p> Article user photo = ' . $article->user->photo . '</p>';
echo '<p> Comments = ' . $article->comments . '</p>';
echo '<hr>';
}
exit();
公共函数getCategory($categoryid){
$cat=cat::where('id',$categoryid)->firstOrFail();
$category=Cat::with('articles')
->with('articles.user'))
->带('articles.comments')
->查找($categoryid)
->第()条
->orderBY('created_at','desc')
->take(Config::get('settings.num_posts'))
->get();
foreach($类别为$物品){
回显“文章正文=”。$Article->body.”;
回显“文章用户=”。$Article->user->name.“”;
回显“文章用户照片=”。$Article->user->photo.“”;
回显“Comments=”.$article->Comments.“”;
回声“
”;
}
退出();
只需在相关模型中使用一个点即可加载
$category = Cat::with('articles')
->with('articles.user')
->with('articles.comments')
->find($categoryid)
->get();
然后访问您的文章
$category->articles;
public function articles() {
return $this->belongsToMany('Article')->with('user');
}
编辑
如果您总是需要每个评论或用户上的用户,那么您总是可以尝试在关系中快速加载,我想您会的
征求意见
public function comments() {
return $this->hasMany('Comment')->with('user');
}
和文章
$category->articles;
public function articles() {
return $this->belongsToMany('Article')->with('user');
}
我也遇到过类似的问题,我通过向模型中添加外键来解决。因此,在您的
注释模型中,最好像这样添加外键id:
class Comment extends Eloquent {
public function article() {
return $this->belongsTo('Article', 'article_id');
}
public function user() {
return $this->belongsTo('User', 'user_id');
}
}
与您的用户模型相同。这种急切的加载仍然不适合我,您能看看上面的编辑和屏幕截图吗?另外,我在尝试访问带有$category->articles“Undefined property”的文章时出错。为什么您有->with('articles')和->articles()两者都有?这可能会导致您编辑的案例出现问题。请尝试删除第二篇文章(),看看这是否有影响。我认为最好在sql上使用JOIN函数。