代码之家  ›  专栏  ›  技术社区  ›  Djura Marinkov

在即时加载中提供额外条件,包括比较两个柱,laravel

  •  1
  • Djura Marinkov  · 技术社区  · 9 年前

    我有学生表、个人资料表、主题表和透视表profile\u主题表

    -配置文件-------{id}

    -profile\u subject--{profile\u id,subject\u id,year}

    类似这样:

    $student = Student::with('profile','profile.subjects')->find(5);
    

    但我也必须插入条件

    ->wherePivot('year','=','students.year')
    

    此查询将不起作用,因为它将搜索哪一年是“学生年”的记录

    1 回复  |  直到 9 年前
        1
  •  1
  •   Alexey Mezenin    9 年前

    使用 lazy eager loading 。此代码不会创建任何其他查询,它将创建与相同数量的查询 with()

    $student = Student::find(5);
    $sudent->load(['profile', 'profile.subjects' => function ($q) use ($student) {
        $q->wherePivot('year', $student->year);
    }]);