intelephense is giving me a syntax error in when writing this code, clearly $category was passed so what is the problem?
public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? false, function ($query, $search) {
$query->where('title', 'like', '%' . $search . '%')->orWhere('body', 'like', '%' . $search . '%');
});
$query->when(
$filters['category'] ?? false,
function ($query, $category) {
$query
->whereExists(
function ($query) {
$query->from('categories')
->whereColumn('categories.id', 'posts.category_id')
->where('categories.slug', $category);
}
);
}
);
}
CodePudding user response:
You can pass the necessary variables from the parent scope into the closure with the use keyword.
$query->when(
$filters['category'] ?? false,
function ($query, $category) {
$query
->whereExists(
function ($query) use ($category) {
$query->from('categories')
->whereColumn('categories.id', 'posts.category_id')
->where('categories.slug', $category);
}
);
}
);