Laravel where has passing additional arguments to function
You have to use use
to pass variables (in your case, $slug
) into the closure (this is called variable inheriting):
public function show($locale, $slug){ $article = Article::whereHas('translations', function ($query) use ($slug) { $query->where('locale', 'en') // ^^^ HERE ->where('slug', $slug); })->first(); return $article;}
If you, in the future, want to pass $locale
in along with it, just comma-separate it:
Article::whereHas('translations', function ($query) use ($slug, $locale) { /* ... */ });
You need to inherit variable from parent scope:
public function show($locale, $slug) { $article = Article::whereHas('translations', function ($query, $slug) use ($slug){ $query->where('locale', 'en') ->where('slug', $slug); })->first(); return $article;}
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.