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

时间:2015-03-14 05:44:22

标签: php laravel-4

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

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

Screenshot of above output

2 个答案:

答案 0 :(得分:0)

只需在相关模型中加载点即可。

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

答案 1 :(得分:0)

我有类似的问题,我通过向我的模型添加外键来解决它。因此,在您的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');
    }
}

与您的用户模型相同。