Home > Back-end >  SQLSTATE[HY093]: Invalid parameter number on query
SQLSTATE[HY093]: Invalid parameter number on query

Time:10-20

I am having trouble getting a number instead of a string in my query. I have two tables and a pivot table.

File:

  • id
  • name
  • document
  • language

Role:

  • id
  • name (admin, partner, dealer, associate)

File_role:

  • id
  • file_id
  • role_id

I want to display all the files for the 'dealer' role so i tried this:

$file_role = File_Role::where('role_id', '=', $id)->get('file_id');
$file = File::where('id', '=', $file_role)->get();
dd($file_role);

However it keeps returning errors on:

$file = File::where('id', '=', $file_role)->get();

The error is:

SQLSTATE[HY093]: Invalid parameter number.

The query below that is:

select * from `file` where `id` = {"file_id":1}.

Any ideas how I can get a number instead of "file_id":1?

CodePudding user response:

You can give this a try. Here you get the first result and then get the file_id from that model.

$file_role = File_Role::where('role_id', '=', $id)->firstOrFail();
$file = File::where('id', '=', $file_role->file_id)->get();

dump($file_role);
dd($file);

CodePudding user response:

I think you are trying to use whereIn:

$file_role = File_Role::where('role_id', '=', $id)->pluck('file_id');
$file = File::whereIn('id', $file_role)->get(); 
  • Related