Skip to content

Commit

Permalink
[5.2] firstOrCreate will not create new db rows when a model has a mu…
Browse files Browse the repository at this point in the history
…tator (#14656)

* firstOrCreate will not create new db rows when a model has a mutator

* removed changes to firstOrCreate in Relationships

* fixed firstOrCreate mutating attribute twice

* replicated behavior of Builder before intial change. No mass assignment error thrown anymore

* fixed typo

* changed Builder edit to pass test. firstOrNew not creating null id when soft deleted user is called from instance

* white space issue

* addedAttributesToArray to declaration of mutatated Attributes

* use getAttributes instead of getAttributesArray
  • Loading branch information
miscbits authored and taylorotwell committed Aug 12, 2016
1 parent dbd0b5f commit df2761f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ public function findOrNew($id, $columns = ['*'])
*/
public function firstOrNew(array $attributes)
{
if (! is_null($instance = $this->where($attributes)->first())) {
$mutatedAttributes = $this->model->newInstance($attributes)->getAttributes();

if (! is_null($instance = $this->where($mutatedAttributes)->first())) {
return $instance;
}

Expand All @@ -248,7 +250,9 @@ public function firstOrNew(array $attributes)
*/
public function firstOrCreate(array $attributes)
{
if (! is_null($instance = $this->where($attributes)->first())) {
$mutatedAttributes = $this->model->newInstance($attributes)->getAttributes();

if (! is_null($instance = $this->where($mutatedAttributes)->first())) {
return $instance;
}

Expand Down

0 comments on commit df2761f

Please sign in to comment.