Laravel 雄辩的多表连接与过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45998387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:35:08  来源:igfitidea点击:

Laravel eloquent multiple table join with filter

phplaravellaravel-5laravel-eloquentlaravel-5.5

提问by I am the Most Stupid Person

There are theree tables in my system.

我的系统中有表。

  1. Students
  2. Articles
  3. categories
  1. 学生们
  2. 文章
  3. 类别

Student can write many articles and a article is belong to just one student. And A Article can have only one category.

学生可以写多篇文章,一篇文章只属于一个学生。并且一篇文章只能有一个类别。

Controller

控制器

public function all_articles_by_student_by_category(Request $request){


        $students_id = $request->students_id;
        $categories_id = $request->categories_id;


        $article_list = Students::find($students_id)->articles->all();

        //This return Something like, Select All Articles Written by Damith
    }

Model

模型

class Students extends Model
{
    protected $fillable = ['id','first_name', 'last_name', 'age', 'created_at', 'updated_at'];

    public function articles()
    {
        return $this->hasMany('App\Articles');
    }
}

What I am try to get

我想得到什么

Something like, Select All Articles Written by Damith for Technology Category (Category Name should be there)

例如,选择 Damith 撰写的所有文章用于技术类别(类别名称应该在那里)

What I able to do so far

到目前为止我能做什么

Something like, Select All Articles Written by Damith using $article_list = Students::find($students_id)->articles->all();(You can find this code from controller)

类似的,选择 Damith 写的所有文章使用$article_list = Students::find($students_id)->articles->all();(您可以从控制器中找到此代码)

What I want from you

我想从你这里得到什么

How do I modify $article_list = Students::find($students_id)->articles->all();to get, something like, Select All Articles Written by Damith for Technology Category. (Category name must be there in result and it is on category table, and for where condtion you can use the category_id which is i the article table)

我如何修改$article_list = Students::find($students_id)->articles->all();以获得诸如选择 Damith 为技术类别撰写的所有文章之类的内容。(类别名称必须在结果中,并且在类别表中,对于条件,您可以使用 category_id 即文章表

回答by kajetons

Something like this should work:

这样的事情应该工作:

$technologyArticles = Articles::where('student_id', '=', $students_id)->where('category_id', '=', $categories_id)->get();

回答by Purgatory

First off with what you have done so farthe ->all()method is not needed when getting the records for a relation on a model, this would return all of the articles linked to that student:

首先,到目前为止->all()在获取模型上关系的记录时不需要该方法,这将返回链接到该学生的所有文章:

Students::find($students_id)->articles


Go through Articles Model
You could do something like:

通过文章模型
您可以执行以下操作:

Article::where('student_id', $students_id)
  ->where('category_id', $category_id)->get();

Which would acheive the result you are after.

这将实现您所追求的结果。



Go through Students Model

通过学生模型

If you want to go through StudentsModelyou can constrain the relation using the withmethod.

如果你想通过,StudentsModel你可以使用with方法约束关系。

$student = Students::with(['articles' => function($query) use ($category_id) {
  $query->where('category_id', $category_id);
}])->find($student_id);

$filteredArticles = $student->articles


Useful Links

有用的链接

  • Laravel 文档 5.5 Eager Loadinghttps: //laravel.com/docs/5.5/eloquent-relationships#eager-loading

When accessing Eloquent relationships as properties, the relationship data is "lazy loaded". This means the relationship data is not actually loaded until you first access the property. However, Eloquent can "eager load" relationships at the time you query the parent model.

当访问 Eloquent 关系作为属性时,关系数据是“延迟加载”的。这意味着在您第一次访问该属性之前不会实际加载关系数据。但是,Eloquent 可以在您查询父模型时“急切加载”关系。

  • Laravel 文档 5.5 Constraining Eager Loadshttps: //laravel.com/docs/5.5/eloquent-relationships#constraining-eager-loads

Sometimes you may wish to eager load a relationship, but also specify additional query constraints for the eager loading query.

有时您可能希望预先加载关系,但也希望为预先加载查询指定额外的查询约束。