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 (laravel#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 tillkruss committed Aug 30, 2016
1 parent bfca5c9 commit 23231a5
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 @@ -234,7 +234,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 @@ -250,7 +252,9 @@ public function firstOrNew(array $attributes)
*/
public function firstOrCreate(array $attributes, array $values = [])
{
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 23231a5

Please sign in to comment.