From bbab8abe98bd43dc3f3882194ec5c8c40d37ed21 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Wed, 18 Jan 2017 19:22:57 +0200 Subject: [PATCH 1/2] move create and forceCreate methods to builder --- src/Illuminate/Database/Eloquent/Builder.php | 34 +++++++++++++++++ src/Illuminate/Database/Eloquent/Model.php | 40 -------------------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 0084f2699f25..af2e58b2820f 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -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 static + */ + 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 static + */ + 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. * diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 0c16be291758..0b2c7ee7e163 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -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. * From 95f2e35d88f320824807b555d80272ee49402b4a Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Wed, 18 Jan 2017 19:27:51 +0200 Subject: [PATCH 2/2] update doc blocks --- src/Illuminate/Database/Eloquent/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index af2e58b2820f..2ef79d4f6136 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -414,7 +414,7 @@ public function firstOr($columns = ['*'], Closure $callback = null) * Save a new model and return the instance. * * @param array $attributes - * @return static + * @return \Illuminate\Database\Eloquent\Model */ public function create(array $attributes = []) { @@ -431,7 +431,7 @@ public function create(array $attributes = []) * Save a new model and return the instance. Allow mass-assignment. * * @param array $attributes - * @return static + * @return \Illuminate\Database\Eloquent\Model */ public function forceCreate(array $attributes) {