Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] firstOrCreate will not create new db rows when a model has a mutator #14656

Merged
merged 9 commits into from
Aug 12, 2016
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