Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 17, 2016
2 parents 505c1a9 + 30866ae commit e78709a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 63 deletions.
79 changes: 79 additions & 0 deletions Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,72 @@ public function findOrFail($id, $columns = ['*'])
throw (new ModelNotFoundException)->setModel(get_class($this->model));
}

/**
* Find a model by its primary key or return fresh model instance.
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model
*/
public function findOrNew($id, $columns = ['*'])
{
if (! is_null($model = $this->find($id, $columns))) {
return $model;
}

return $this->newModel();
}

/**
* Get the first record matching the attributes or instantiate it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrNew(array $attributes)
{
if (! is_null($instance = $this->where($attributes)->first())) {
return $instance;
}

return $this->newModel($attributes);
}

/**
* Get the first record matching the attributes or create it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrCreate(array $attributes)
{
if (! is_null($instance = $this->where($attributes)->first())) {
return $instance;
}

$instance = $this->newModel($attributes);

$instance->save();

return $instance;
}

/**
* Create or update a record matching the attributes, and fill it with values.
*
* @param array $attributes
* @param array $values
* @return \Illuminate\Database\Eloquent\Model
*/
public function updateOrCreate(array $attributes, array $values = [])
{
$instance = $this->firstOrNew($attributes);

$instance->fill($values)->save();

return $instance;
}

/**
* Execute the query and get the first result.
*
Expand Down Expand Up @@ -1183,6 +1249,19 @@ public function getModel()
return $this->model;
}

/**
* Get a fresh instance of a model instance being queried.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function newModel(array $attributes = [])
{
$class = get_class($this->model);

return new $class($attributes);
}

/**
* Set a model instance for the model being queried.
*
Expand Down
63 changes: 0 additions & 63 deletions Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,53 +590,6 @@ public static function forceCreate(array $attributes)
});
}

/**
* Get the first record matching the attributes or create it.
*
* @param array $attributes
* @return static
*/
public static function firstOrCreate(array $attributes)
{
if (! is_null($instance = (new static)->newQueryWithoutScopes()->where($attributes)->first())) {
return $instance;
}

return static::create($attributes);
}

/**
* Get the first record matching the attributes or instantiate it.
*
* @param array $attributes
* @return static
*/
public static function firstOrNew(array $attributes)
{
if (! is_null($instance = (new static)->newQueryWithoutScopes()->where($attributes)->first())) {
return $instance;
}

return new static($attributes);
}

/**
* Create or update a record matching the attributes, and fill it with values.
*
* @param array $attributes
* @param array $values
* @param array $options
* @return static
*/
public static function updateOrCreate(array $attributes, array $values = [], array $options = [])
{
$instance = static::firstOrNew($attributes);

$instance->fill($values)->save($options);

return $instance;
}

/**
* Begin querying the model.
*
Expand Down Expand Up @@ -692,22 +645,6 @@ public static function all($columns = ['*'])
return $instance->newQuery()->get($columns);
}

/**
* Find a model by its primary key or return new static.
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|static
*/
public static function findOrNew($id, $columns = ['*'])
{
if (! is_null($model = static::find($id, $columns))) {
return $model;
}

return new static;
}

/**
* Reload a fresh model instance from the database.
*
Expand Down

0 comments on commit e78709a

Please sign in to comment.