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] Some refactoring on Eloquent builder #18775

Merged
merged 1 commit into from
Apr 11, 2017
Merged
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
71 changes: 32 additions & 39 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function whereKey($id)
*
* @param string|\Closure $column
* @param string $operator
* @param mixed $value
* @param mixed $value
* @param string $boolean
* @return $this
*/
Expand All @@ -209,7 +209,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
*
* @param string|\Closure $column
* @param string $operator
* @param mixed $value
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function orWhere($column, $operator = null, $value = null)
Expand All @@ -225,9 +225,7 @@ public function orWhere($column, $operator = null, $value = null)
*/
public function hydrate(array $items)
{
$instance = $this->model->newInstance()->setConnection(
$this->query->getConnection()->getName()
);
$instance = $this->newModelInstance();

return $instance->newCollection(array_map(function ($item) use ($instance) {
return $instance->newFromBuilder($item);
Expand All @@ -243,10 +241,8 @@ public function hydrate(array $items)
*/
public function fromQuery($query, $bindings = [])
{
$instance = $this->model->newInstance();

return $this->hydrate(
$instance->getConnection()->select($query, $bindings)
$this->query->getConnection()->select($query, $bindings)
);
}

Expand Down Expand Up @@ -321,9 +317,7 @@ public function findOrNew($id, $columns = ['*'])
return $model;
}

return $this->model->newInstance()->setConnection(
$this->query->getConnection()->getName()
);
return $this->newModelInstance();
}

/**
Expand All @@ -339,9 +333,7 @@ public function firstOrNew(array $attributes, array $values = [])
return $instance;
}

return $this->model->newInstance($attributes + $values)->setConnection(
$this->query->getConnection()->getName()
);
return $this->newModelInstance($attributes + $values);
}

/**
Expand All @@ -357,13 +349,9 @@ public function firstOrCreate(array $attributes, array $values = [])
return $instance;
}

$instance = $this->model->newInstance($attributes + $values)->setConnection(
$this->query->getConnection()->getName()
);

$instance->save();

return $instance;
return tap($this->newModelInstance($attributes + $values), function ($instance) {
$instance->save();
});
}

/**
Expand Down Expand Up @@ -732,13 +720,9 @@ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'p
*/
public function create(array $attributes = [])
{
$instance = $this->model->newInstance($attributes)->setConnection(
$this->query->getConnection()->getName()
);

$instance->save();

return $instance;
return tap($this->newModelInstance($attributes), function ($instance) {
$instance->save();
});
}

/**
Expand All @@ -749,12 +733,8 @@ public function create(array $attributes = [])
*/
public function forceCreate(array $attributes)
{
$instance = $this->model->newInstance()->setConnection(
$this->query->getConnection()->getName()
);

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

Expand Down Expand Up @@ -921,8 +901,8 @@ public function applyScopes()
/**
* Apply the given scope on the current builder instance.
*
* @param callable $scope
* @param array $parameters
* @param callable $scope
* @param array $parameters
* @return mixed
*/
protected function callScope(callable $scope, $parameters = [])
Expand Down Expand Up @@ -1039,6 +1019,19 @@ public function without($relations)
return $this;
}

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

/**
* Parse a list of relations into individuals.
*
Expand Down Expand Up @@ -1091,7 +1084,7 @@ protected function createSelectWithConstraint($name)
* Parse the nested relationships in a relation.
*
* @param string $name
* @param array $results
* @param array $results
* @return array
*/
protected function addNestedWiths($name, $results)
Expand Down Expand Up @@ -1210,7 +1203,7 @@ public function getMacro($name)
* Dynamically handle calls into the query instance.
*
* @param string $method
* @param array $parameters
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
Expand Down Expand Up @@ -1252,7 +1245,7 @@ public function __call($method, $parameters)
* Dynamically handle calls into the query instance.
*
* @param string $method
* @param array $parameters
* @param array $parameters
* @return mixed
*
* @throws \BadMethodCallException
Expand Down