繁体   English   中英

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

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

如何通过文章用户,评论和评论用户急切加载属于某个类别的文章?

Laravel版本是4.2.17

使用查询事件侦听器,并将sql查询转储到页面。 HomeContoller方法工作正常,急切加载所有文章没有N + 1问题。

但是,getCategory方法加载所有文章,但在foreach循环中执行sql查询以获取文章用户,注释和注释用户。

- 数据库表

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

}

- 控制器

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

以上输出的屏幕截图

只需使用点加载相关模型即可。

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

然后访问您的文章。

$category->articles;

编辑

如果你总是在每个评论或用户上需要用户,你可以随时尝试在关系中加载。我认为你会这样做。

征求意见

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

对于文章。

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

我有类似的问题,我通过在我的模型中添加一个外键来解决它。 所以在你的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');
    }
}

与您的用户模型相同。

暂无
暂无

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

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