paklic9
112
2
General

Undefined variable in function with query

Posted 1 year ago by paklic9

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

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.