简体   繁体   English

雄辩的关系和多维集合拒绝

[英]eloquent relation and multi dimensional collection reject

I have a school database which hasMany teacher, and which hasMany student like this: 我有一个学校数据库,其中有很多老师,有很多学生,像这样:

class School extends Model
{
    public function teachers()
    {
        return $this->hasMany(Teacher::class,'school_id');
    }
}

class Teacher extends Model
{
    public function students()
    {
        return $this->hasMany(Student::class,'teacher_id');
    }
}

class Student extends Model
{
}

The full query 完整查询

$full = School::with(['teachers' => function ($query) {
    $query->with('students');
}])->get();

And the result is like this: 结果是这样的:

[
    {
        "id": 1,
        "school_name": "First Park",
        "teachers": [
            {
                "id": 1,
                "teacher_name": "Mr.Aha",
                "students": [
                    {
                        "id": 1,
                        "student_name": "Jane",
                        "drop": 1
                    },
                    {
                        "id": 2,
                        "student_name": "Jon",
                        "drop": 0
                    }
                ]
            }
        ]
    }
]

Now I want to remove student who drop school and select teacher_name, so I try to do it with eloquent and collection, but they all fail. 现在,我想删除辍学的学生,并选择Teacher_name,因此我尝试用雄辩和收集来做到这一点,但他们都失败了。

The eloquent way is: 雄辩的方法是:

School::with(['teachers' => function ($teacher) {
    $teacher->with(['students' => function ($student) {
        $student->where('drop', '!=', 0)
    }])->select('teacher_name');
}])->get();

But the result output, teacher is an empty object. 但是结果输出,老师是一个空对象。


The collection way is base on the full query 收集方式基于完整查询

$full->map(function ($teacher) {
    unset($teacher->id);//there are more column to unset in real life
    $teacher->reject(function ($student) {
        return $student->drop == 0;
    });
});

But the result is the same as full query. 但是结果与完整查询相同。


I don't know which way is better, eloquent or collection, but they won't work 我不知道哪种方法更好,更有说服力或有收藏价值,但它们不会起作用

You also have to select the id and school_id columns that are required for eager loading: 您还必须选择渴望加​​载所需的idschool_id列:

School::with(['teachers' => function ($teacher) {
    $teacher->with(['students' => function ($student) {
        $student->where('drop', '!=', 0)
    }])->select('id', 'school_id', 'teacher_name');
}])->get();

Then remove them afterwards or use $hidden : 然后将其删除,或使用$ hidden

$full->map(function ($teacher) {
    unset($teacher->id, $teacher->school_id);
});

Example from original docs. 来自原始文档的示例。

Nested Eager Loading To eager load nested relationships, you may use "dot" syntax. 嵌套的预先加载要渴望加载嵌套的关系,可以使用“点”语法。 For example, let's eager load all of the book's authors and all of the author's personal contacts in one Eloquent statement: 例如,让我们急切地在一项雄辩的陈述中加载本书的所有作者和所有作者的个人联系人:

$books = App\\Book::with('author.contacts')->get(); $ books = App \\ Book :: with('author.contacts')-> get();

also, you can use load() method 另外,您可以使用load()方法

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

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