使用此热切加载:
$lang = in_array($lang,$languages) ? $lang : 'en';
$requirements = Requirement::with([
'countryMatch',
'applier' => function ($query) { $query->select('id','passport_type','office_id','citizenship_id')->groupBy('citizenship_id');},
'doc.translation' => function ($query) { $query->where('language',$lang);}])->get();
我正在尝试使用'with statement'中开头定义的变量$ lang,但我总是得到:
Undefined variable: lang
答案 0 :(得分:1)
尝试此操作,使用use
关键字将变量传递给闭包:
$requirements = Requirement::with([
'countryMatch',
'applier' => function ($query) {
$query->select('id','passport_type','office_id','citizenship_id')->groupBy('citizenship_id');
},
'doc.translation' => function ($query) use ($lang) {
$query->where('language',$lang);
}
])->get();