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.4] Move model methods to builder #17395

Merged
merged 2 commits into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,40 @@ public function firstOr($columns = ['*'], Closure $callback = null)
return call_user_func($callback);
}

/**
* Save a new model and return the instance.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function create(array $attributes = [])
{
$instance = $this->model->newInstance($attributes)->setConnection(
$this->query->getConnection()->getName()
);

$instance->save();

return $instance;
}

/**
* Save a new model and return the instance. Allow mass-assignment.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function forceCreate(array $attributes)
{
$instance = $this->model->newInstance($attributes)->setConnection(
$this->query->getConnection()->getName()
);

return $this->model->unguarded(function () use ($attributes, $instance) {
return $instance->create($attributes);
});
}

/**
* Get a single column's value from the first result of a query.
*
Expand Down
40 changes: 0 additions & 40 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,46 +320,6 @@ public static function onWriteConnection()
return $instance->newQuery()->useWritePdo();
}

/**
* Save a new model and return the instance.
*
* @param array $attributes
* @param string|null $connection
* @return static
*/
public static function create(array $attributes = [], $connection = null)
{
$instance = new static($attributes);

if ($connection) {
$instance->setConnection($connection);
}

return tap($instance, function ($model) {
$model->save();
});
}

/**
* Save a new model and return the instance. Allow mass-assignment.
*
* @param array $attributes
* @param string|null $connection
* @return static
*/
public static function forceCreate(array $attributes, $connection = null)
{
$instance = (new static);

if ($connection) {
$instance->setConnection($connection);
}

return static::unguarded(function () use ($attributes, $instance) {
return $instance->create($attributes);
});
}

/**
* Create a collection of models from plain arrays.
*
Expand Down