Laravel'具有'急切的装载范围

时间:2017-07-04 18:44:26

标签: php laravel

使用此热切加载:

$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

1 个答案:

答案 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();