Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 雄辩关系与多维集合_Php_Laravel_Eloquent - Fatal编程技术网

Php 雄辩关系与多维集合

Php 雄辩关系与多维集合,php,laravel,eloquent,Php,Laravel,Eloquent,我有一个学校数据库,有很多老师,也有很多学生,就像这样: class School extends Model { public function teachers() { return $this->hasMany(Teacher::class,'school_id'); } } class Teacher extends Model { public function students() { return $t

我有一个学校数据库,有很多老师,也有很多学生,就像这样:

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
{
}
[
    {
        "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
                    }
                ]
            }
        ]
    }
]

完整查询

$full = School::with(['teachers' => function ($query) {
    $query->with('students');
}])->get();
$full->map(function ($teacher) {
    unset($teacher->id);//there are more column to unset in real life
    $teacher->reject(function ($student) {
        return $student->drop == 0;
    });
});
结果是这样的:

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
{
}
[
    {
        "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
                    }
                ]
            }
        ]
    }
]

现在我想删除那个些辍学的学生并选择老师的名字,所以我试着用雄辩和收集的方式来做,但他们都失败了

雄辩的方式是:

School::with(['teachers' => function ($teacher) {
    $teacher->with(['students' => function ($student) {
        $student->where('drop', '!=', 0)
    }])->select('teacher_name');
}])->get();
但结果输出,教师是一个空的对象


收集方式基于完整查询

$full = School::with(['teachers' => function ($query) {
    $query->with('students');
}])->get();
$full->map(function ($teacher) {
    unset($teacher->id);//there are more column to unset in real life
    $teacher->reject(function ($student) {
        return $student->drop == 0;
    });
});
但结果与完整查询相同


我不知道哪种方式更好,是雄辩的方式还是收集的方式,但它们不起作用。

来自原始文档的示例。 嵌套急加载 要加载嵌套关系,可以使用“点”语法。例如,让我们把这本书的所有作者和作者的所有个人联系都放在一个雄辩的陈述中:

$books=App\Book::with('author.contacts')->get();
此外,您还可以使用load()方法

您还必须选择急于加载所需的
id
school\u id
列:

School::with(['teachers' => function ($teacher) {
    $teacher->with(['students' => function ($student) {
        $student->where('drop', '!=', 0)
    }])->select('id', 'school_id', 'teacher_name');
}])->get();
然后将其取出或使用:


谢谢,当选择“id”和其他列时,它会工作。