Skip to content

Commit 34430da

Browse files
committed
different approach
1 parent 89568c7 commit 34430da

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/Illuminate/Database/Eloquent/Builder.php

+25-12
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ public function findOr($id, $columns = ['*'], ?Closure $callback = null)
615615
'insertusing',
616616
'insertorignoreusing',
617617
*/
618+
/**
619+
* Insert new records into the database.
620+
*
621+
* @return bool
622+
*/
618623
public function insert(array $values)
619624
{
620625
if ($this->mergeAttributesBeforeInsert) {
@@ -625,9 +630,10 @@ public function insert(array $values)
625630
}
626631

627632
/**
628-
* @param array $values
629-
* @param $sequence
630-
* @return int|mixed
633+
* Insert a new record and get the value of the primary key.
634+
*
635+
* @param string|null $sequence
636+
* @return int
631637
*/
632638
public function insertGetId(array $values, $sequence = null)
633639
{
@@ -638,17 +644,31 @@ public function insertGetId(array $values, $sequence = null)
638644
return $this->forwardCallTo($this->query, 'insertGetId', [$values, $sequence]);
639645
}
640646

647+
/**
648+
* Insert new records into the database while ignoring errors.
649+
*
650+
* @return int
651+
*/
652+
public function insertOrIgnore(array $values)
653+
{
654+
if ($this->mergeAttributesBeforeInsert) {
655+
$values = $this->castBeforeInsert($values);
656+
}
657+
658+
return $this->forwardCallTo($this->query, 'insertGetId', [$values]);
659+
}
660+
641661
/**
642662
* Insert a number of records, merging in default attributes,
643663
* adding timestamps, and converting casts to raw values.
644664
*
645-
* @param list<array<string, mixed>> $values
665+
* @param array<int, <array<string, mixed>> $values
646666
* @return array
647667
*/
648668
public function castBeforeInsert($values)
649669
{
650670
if (empty($values)) {
651-
[];
671+
return [];
652672
}
653673

654674
if (! is_array(reset($values))) {
@@ -2244,13 +2264,6 @@ public function __call($method, $parameters)
22442264
return $this->callNamedScope($method, $parameters);
22452265
}
22462266

2247-
if (in_array($lowerCaseMethod = strtolower($method), $this->passthru)) {
2248-
if ($this->mergeAttributesBeforeInsert && in_array($lowerCaseMethod, $this->mergeBeforeInsertPassThru)) {
2249-
$parameters[0] = $this->castBeforeInsert($parameters[0])[0];
2250-
}
2251-
return $this->toBase()->{$method}(...$parameters);
2252-
}
2253-
22542267
$this->forwardCallTo($this->query, $method, $parameters);
22552268

22562269
return $this;

0 commit comments

Comments
 (0)