简体   繁体   English

Laravel - 如何渴望加载多对多的关系

[英]Laravel - How to eager load many to many relationships

How would I eager load articles that belong to a category with article user, comments and comments user ? 如何通过文章用户,评论和评论用户急切加载属于某个类别的文章?

Laravel version is 4.2.17 Laravel版本是4.2.17

Using the query event listener, and dumping sql queries to the page. 使用查询事件侦听器,并将sql查询转储到页面。 The HomeContoller method works fine, eager loads all of the articles without the N+1 problem. HomeContoller方法工作正常,急切加载所有文章没有N + 1问题。

However the getCategory method loads all the articles ok but does an sql query inside the foreach loop to get the article user, comments and comments user. 但是,getCategory方法加载所有文章,但在foreach循环中执行sql查询以获取文章用户,注释和注释用户。

-- Database tables - 数据库表

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

-- Models - 楷模

class Article extends Eloquent {

    public function user() {
        return $this->belongsTo('User');
    }


    public function comments() {
        return $this->hasMany('Comment');
    }

}


class Comment extends Eloquent {

    public function article() {
        return $this->belongsTo('Article');
    }

    public function user() {
        return $this->belongsTo('User');
    }

}

class User extends Eloquent implements UserInterface, RemindableInterface {

    public function articles() {
        return $this->hasMany('article');
    }

    public function comments() {
        return $this->hasMany('comment');
    }

}


class Cat extends Eloquent {

    public function articles() {
        return $this->belongsToMany('Article');
    }

}

-- Controllers - 控制器

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();
    }

}

Edit 编辑

Have made some changes to the controller but it's still not eager loading, below is the new controller ... 已经对控制器进行了一些更改,但它仍然没有急于加载,下面是新的控制器......

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();

以上输出的屏幕截图

Just use a dot to load in the related models. 只需使用点加载相关模型即可。

$category = Cat::with('articles')
    ->with('articles.user')
    ->with('articles.comments')
    ->find($categoryid)
    ->get();

And then access your articles. 然后访问您的文章。

$category->articles;

Edit 编辑

You can always try eager loading within the relationship if you're always going to need the user on each comment or user, which I assume you will. 如果你总是在每个评论或用户上需要用户,你可以随时尝试在关系中加载。我认为你会这样做。

For comments. 征求意见

public function comments() {
   return $this->hasMany('Comment')->with('user');
}

And for articles. 对于文章。

public function articles() {
    return $this->belongsToMany('Article')->with('user');
}

I've had similar issues and I resolved it by adding a foreign key to my models. 我有类似的问题,我通过在我的模型中添加一个外键来解决它。 So in your Comment model, it's best to add your foreign_key id like this: 所以在你的Comment模型中,最好像这样添加你的foreign_key id:

class Comment extends Eloquent {

    public function article() {
        return $this->belongsTo('Article', 'article_id');
    }

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

Same with your user model. 与您的用户模型相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM