I have a SELECT that evaluates two conditions, the first of them is mandatory and the second is a subgroup where at least one should give "true" so that the general SELECT is fulfilled. The thing is that according to the recommendations of the ELOQUENT documentation, in these cases the best option is to pass a "closure" as the second parameter (the group of options) but the thing is that even in there I have to send a variable to validate and when I try to do it, the "closure" function fails and throws an error. This is what I have so far.
$modelo = Escuela::where("nombre", $nombre)
->where(function($q, $b){
$q->where("apellido","like","%" . $b . "%")
->orWhere("ciudad","like","%" . $b . "%");
})->select("id","nombre","apellido")->get();
Assuming one the values:
$nombre = "John"
$b = "gua"
It should be more or less like this:
select * from users where name = 'John' and (apellido LIKE '%gua%' or ciudad LIKE '%gua%'),
But it throws me an error saying that the number of arguments of the "closure" is not correct
What am I missing?
I don't think it's the best solution but I couldn't get it to work trying to pass a variable... so the "sticky" solution I used was to declare a constant before doing the query and put that constant inside the query:
It seems to me that the only thing missing was passing your variables to the function context like this:
And your query would look like this: