简体   繁体   English

模型函数中的Laravel联合

[英]Laravel union within model functions

Hello i am new here and not a native English speaker so please forgive me for any mistakes on my grammar and on my question formatting. 您好,我是新来的,不是英语为母语的人,所以请原谅我在语法和问题格式方面的任何错误。

I am building an app with php using laravel framework 5.4 version. 我正在使用laravel framework 5.4版本使用php构建应用程序。 The web app is very simple its for reviewing articles and users that posts articles. 该Web应用程序非常简单,用于审阅文章和发布文章的用户。

I would like to learn how i can union the results of the functions within my model. 我想学习如何在模型中合并函数的结果。 I want the allReviews function from user model to return the reviews the user has mixed with the reviews his articles have orderby createdtime. 我希望用户模型中的allReviews函数返回用户混合的评论以及他的文章按创建时间排序的评论。

let me explain better. 让我解释得更好。

here is my 3 main tables: 这是我的3个主要表格:

Users     | Articles  | Reviews
--------- | --------- | --------- 
id        | id        | id
name      | user_id   | reviewable_id
email     | title     | reviewable_type
password  | body      | reviewtext
etc..     | etc..     | created_time

and here is my models code : 这是我的模型代码:

class User extends Model{

    protected $table = 'users';

    public function articles()
    {
        return $this->hasMany(Article::class,'user_id');
    }

    public function reviews(){
        return $this->morphMany(Review::class,'reviewable');
    }

    public function allReviews(){
        /*
         i want union something like this:

        $result = $this->reviews() union
        foreach ($this->Articles() as $Article) {
            union $Article->reviews();
        }
        orderby created_time ASC or DESC doesn't matter

        return $result
        */
    }
}

class Article extends Model{

    protected $table = 'articles';

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

    public function reviews(){
        return $this->morphMany(Review::class,'reviewable');
    }
}

class Review extends Model{

    protected $table = 'reviews';

    public function reviewable(){
        return $this->morphTo('reviewable');
    }
}

So my question is how i can do the function allReviews from user to work ? 所以我的问题是我该如何执行用户的allReviews功能呢?

Any help is appreciated :) Thank You 任何帮助表示赞赏:)谢谢

Calling the $user->reviews property will return all reviewable models. 调用$ user-> reviews属性将返回所有可审查模型。 You don't have to UNION anything, Eloquent will take care of that for you. 您不需要任何UNION,Eloquent会为您解决。

Try this: 尝试这个:

public function allReviews(){
    $reviews = new \Illuminate\Database\Eloquent\Collection;

    foreach($this->articles as $article)
    {
        $reviews = $reviews->merge($article->reviews);
    }

    $reviews = $reviews->merge($this->reviews);

    return $reviews;
}

I'm really tired now and I have a feeling you might get the N+1 query problem situation here but it should work for you. 我现在真的很累,我觉得您可能会遇到N + 1查询问题的情况,但它应该对您有用。

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

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