Wherein no Eloquent

Asked

Viewed 11,610 times

2

I’m trying to implement a query with the use of whereIn in Eloquent, but always generates a mistake, someone would know to give me guidance on?

My code is like this:

$result = DB::query()->select('permissao')
                     ->whereIn('id_permissoes', $variavel_com_os_valores)
                     ->get();

I’ve tried passing the variable inside a array also [$variavel_com_os_valores], but always creates error, someone knows if there is another way to implement a where in? Even if it is in pure SQL SELECT * FROM tabela WHERE id IN ($variavel)?

When it does not generate an error message only brings a return response, that is, it can only check the first number that is contained in the variable, let’s say the variable contains 1,2,3 only check in the first paragraph, in case 1.

When it generates the error message is the following:

Warning: Invalid argument supplied for foreach() in D: PROJETOS clinicamedica vendor Illuminate database Query Builder.php on line 775

Fatal error: Uncaught Typeerror: Argument 1 passed to Illuminate Database Grammar::parameterize() must be of the type array, string Given, called in D: PROJETOS clinicamedica vendor Illuminate database Query Grammars Grammar.php on line 250 and defined in D: PROJETOS clinicamedica vendor Illuminate database Grammar.php:135 Stack trace: #0 D: PROJETOS clinicamedica vendor Illuminate database Query Grammars Grammar.php(250): Illuminate Database Grammar->parameterize('1, 2') #1 D: PROJETOS clinicamedica vendor Illuminate database Query Grammars Grammar.php(196): Illuminate Database Query Grammars Grammar->wherein(Object(Illuminate Database Query Builder), Array) #2 [Internal Function]: Illuminate Database Query Grammars Grammar->Illuminate Database Query Grammars{closure}(Array, 0) #3 D: PROJETOS clinicamedica vendor Illuminate support Collection.php(743): array_map(Object(Closure), Array, Array) #4 D: PROJETOS clinicamedica vendor Illuminate database Query Grammars Grammar.php(197): Illuminate Support Collection->map(Objec in D: PROJETOS clinicamedica vendor Illuminate database Grammar.php on line 135`

  • First: whereIn expecting a array as parameter, so yes, you need to pass it as array. The question now is: What is the value of $variavel_com_os_valores? Is a string? If it is, you’ll need to give the explode in it.

  • $variavel_com_os_valores has what? and will always have that value ?

  • That’s right, I decided by applying the explode....

1 answer

7


According to the documentation of Larable, the "wherein" method should receive an array as a parameter, even if it is only a value.

$result = DB::query()->select( 'permissao' )
                     ->whereIn('id_permissoes', $array )
                     ->get();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.