Level 46
Pass $filtry into the anonymous function with use .
$inzerce = \DB::table($tabulka)
->where(function($query) use($filtry) {
...
}
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I have problem with my function, error is: "Undefined variable: filtry". I know why, the variable $filtry have to be defined in query function (like variable $j is), then it is working. But I cannot have this variable static, because variable $filtry will be always diffrent, so I need set it before function, then call it in function. Is it possible?
Thanks in advance for help.
My function:
function dbselect($tabulka, $filtry){
$inzerce = \DB::table($tabulka)
->where(function($query){
$j = 0;
foreach ($filtry as $filtr) {
$filtr = \Request::has($filtr) ? \Request::get($filtr) : [];
if(isset($filtr)){
$i = 0;
foreach ($filtr as $polozka) {
if ($i == 0) {
$query->where($filtry[$j],'=', $polozka);
$i++;
}
else{
$query->orwhere($filtry[$j],'=', $polozka);
$i++;
}
}
}
$j++;
}
})->get();
}
Calling function:
$filtry = ['kategorie'];
dbselect('zviratas', $filtry);
Pass $filtry into the anonymous function with use .
$inzerce = \DB::table($tabulka)
->where(function($query) use($filtry) {
...
}
Please or to participate in this conversation.