Weird issue with Laravel bindings #36284
-
Hi there, I wasn't sure if this was a Laravel issue or not but I thought I might as well ask here first before making an issue. Now for the issue. No matter what I pass into my bindings, Here's the code in question: /**
* Add additional constraints to the builder based on the given argument value.
*
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $builder
* @param mixed $value
*
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
*/
public function handleBuilder($builder, $value)
{
$relation = (string) $this->directiveArgValue('relation', $this->nodeName());
$where = (array) $this->directiveArgValue('where');
if (null !== $value) {
foreach ($value as $search) {
$builder = $builder->whereHas($relation, function (\Illuminate\Database\Eloquent\Builder $query) use ($where, $search) {
foreach ($where as $condition) {
\Log::debug($condition['key'].' '.$search[$condition['key']]);
if (array_key_exists($condition['key'], $search)) {
\Log::debug($condition['key'].' '.$search[$condition['key']].' '.gettype($search[$condition['key']]));
$key = $condition['key'];
$value = $search[$key];
$query->where($key, $condition['operator'] || 'LIKE', $value, 'and');
} else {
\Log::debug("Doesn't exist?");
}
}
});
}
\Log::debug($builder->toSql());
\Log::debug(var_export($builder->getBindings(), true));
return $builder;
} else {
return $builder;
}
} (yes, the code is a mess, I haven't gotten around to cleaning up the code yet since it isn't even working at the moment)
I hope I can get a bit of insight into this problem if it's an obvious thing I didn't see. Thanks for your time! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I just updated Laravel to v8.28.0 after seeing that there was an advisory about query builder issues (GHSA-x7p5-p2c9-phvg) but the issue still appears |
Beta Was this translation helpful? Give feedback.
-
For me in:
this part |
Beta Was this translation helpful? Give feedback.
For me in:
this part
$condition['operator'] || 'LIKE'
is invalid. It probably always returnstrue
. Instead it should be probably something like$condition['operator'] ?? 'LIKE'