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

using this eager loading:

$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();

I am trying to use the variable $lang defined at the beginning inside the 'with statement' but I always get:

Undefined variable: lang

Try this, pass your variable by using the use keyword to the closure:

$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();