Home > Software design >  Laravel how to ignore empty parameter numbers
Laravel how to ignore empty parameter numbers

Time:10-26

I am making a laravel application, Here i can 'filter' out my 'files' first by role (assigned to the user), Folder and then subfolders. for the query i use a for loop, but this can return different values depending on the 'filters' i use. Now laravel returns this error message:

SQLSTATE[HY093]: Invalid parameter number

Query:

select * from `file` where `id` in (8, 12, 13, ?, ?, ?)

Now i want to ignore those '?' and keep it from returning an error, i know this is doable by making the '?' NULL. but i cant find a way to do that

the code:

 public function show($id)
    {
   
        $user = Auth::user();
        $userid = $user->id;
        $role_id = $user->role_id;
        $folders = Folder::all();
        $file_role = File_Role::where('role_id', '=', $role_id)->pluck('file_id');
    //    dd($file_role[1]);
    $count = count($file_role);
    $i = 0;
 
  
    var_dump($file_role);
        for($i = 0; $i < $count; $i  ) {
          
        $file[] = File_Subfolder::where('subfolder_id', '=', $id)->where('file_id', '=', $file_role[$i])->pluck('file_id');
        
        }  

        $chosenfile = File::whereIn('id', $file)->get();
        
        return view('partner.subquents.sub_file', compact('chosenfile'));
    }

$file returns 8,12,13,?,?,? in this case. this can change depending on the filters

i tried an foreach loop.and i tried to find on the internet how to assign NULL to '?' values

as you can see in my DD that some dont give data back, but some do

enter image description here

CodePudding user response:

Can u try this:

$fileSubFolderCount = File_Subfolder::where('subfolder_id', '=', $id)->where('file_id', '=', $file_role[$i])->pluck('file_id')->count();
$file[] = $fileSubFolderCount > 0 ? File_Subfolder::where('subfolder_id', '=', $id)->where('file_id', '=', $file_role[$i])->pluck('file_id') : null;
  • Related