Skip to content

Commit

Permalink
#6718 Revisit illuminate/database binding fix (stable-3_2_0)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher committed Feb 4, 2021
1 parent 54c57ad commit ee67a9d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -42,7 +42,8 @@
"extra": {
"patches": {
"illuminate/database": {
"Patch Laravel binding array issue": "lib/laravel-binding-6632.diff"
"Patch Laravel binding array issue": "lib/laravel-binding-6632.diff",
"Patch Laravel binding array issue v2": "lib/laravel-binding-6718.diff"
}
}
}
Expand Down
111 changes: 111 additions & 0 deletions lib/laravel-binding-6718.diff
@@ -0,0 +1,111 @@
diff -r -u a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php
--- a/src/Illuminate/Database/Query/Builder.php
+++ b/src/Illuminate/Database/Query/Builder.php
@@ -293,6 +293,17 @@
}

/**
+ * Returns scalar type value from an unknown type of input.
+ *
+ * @param mixed $value
+ * @return mixed
+ */
+ protected function scalarValue($value)
+ {
+ return is_array($value) ? head(Arr::flatten($value)) : $value;
+ }
+
+ /**
* Creates a subquery and parse it.
*
* @param \Closure|\Illuminate\Database\Query\Builder|string $query
@@ -658,7 +669,7 @@
);

if (! $value instanceof Expression) {
- $this->addBinding(is_array($value) ? head($value) : $value, 'where');
+ $this->addBinding($this->scalarValue($value), 'where');
}

return $this;
@@ -1050,7 +1061,7 @@

$this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not');

- $this->addBinding(array_slice($this->cleanBindings($values), 0, 2), 'where');
+ $this->addBinding(array_slice($this->cleanBindings(Arr::flatten($values)), 0, 2), 'where');

return $this;
}
@@ -1118,7 +1129,7 @@
$value, $operator, func_num_args() === 2
);

- $value = is_array($value) ? head($value) : $value;
+ $value = $this->scalarValue($value);

if ($value instanceof DateTimeInterface) {
$value = $value->format('Y-m-d');
@@ -1159,7 +1170,7 @@
$value, $operator, func_num_args() === 2
);

- $value = is_array($value) ? head($value) : $value;
+ $value = $this->scalarValue($value);

if ($value instanceof DateTimeInterface) {
$value = $value->format('H:i:s');
@@ -1200,7 +1211,7 @@
$value, $operator, func_num_args() === 2
);

- $value = is_array($value) ? head($value) : $value;
+ $value = $this->scalarValue($value);

if ($value instanceof DateTimeInterface) {
$value = $value->format('d');
@@ -1245,7 +1256,7 @@
$value, $operator, func_num_args() === 2
);

- $value = is_array($value) ? head($value) : $value;
+ $value = $this->scalarValue($value);

if ($value instanceof DateTimeInterface) {
$value = $value->format('m');
@@ -1290,7 +1301,7 @@
$value, $operator, func_num_args() === 2
);

- $value = is_array($value) ? head($value) : $value;
+ $value = $this->scalarValue($value);

if ($value instanceof DateTimeInterface) {
$value = $value->format('Y');
@@ -1598,7 +1609,7 @@
$this->wheres[] = compact('type', 'column', 'operator', 'value', 'boolean');

if (! $value instanceof Expression) {
- $this->addBinding((int) $value);
+ $this->addBinding((int) $this->scalarValue($value));
}

return $this;
@@ -1731,7 +1742,7 @@
$this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean');

if (! $value instanceof Expression) {
- $this->addBinding(is_array($value) ? head($value) : $value, 'having');
+ $this->addBinding($this->scalarValue($value), 'having');
}

return $this;
@@ -1769,7 +1780,7 @@

$this->havings[] = compact('type', 'column', 'values', 'boolean', 'not');

- $this->addBinding($this->cleanBindings($values), 'having');
+ $this->addBinding(array_slice($this->cleanBindings(Arr::flatten($values)), 0, 2), 'having');

return $this;
}

0 comments on commit ee67a9d

Please sign in to comment.