make creates a model instance BUT does not persist it to the database i.e. model is not saved untill you call the save method.
Is make going to be deprecated
I don't think so because make method is usually used during unit tests, at least that's how I use it. Where I just want to make a user and run some validation tests on it before saving it to db.
You can follow the paths of make and __construct to see how they are broadly similar. The make method is particularly useful if you wished to fluently chain further methods on the Model instance, whereas new typically requires assignment to a temporary variable (not strictly necessary either, but that depends on your code styling preference).
@gaiustemple@pilat nope, I don't think so. I believe the make method is a more advanced way to create a new model instance and should be the recommended way.
Looking at the source code:
// Illuminate\Database\Eloquent\Builder
public $pendingAttributes = [];
public function make(array $attributes = [])
{
return $this->newModelInstance($attributes);
}
public function newModelInstance($attributes = [])
{
$attributes = array_merge($this->pendingAttributes, $attributes);
return $this->model->newInstance($attributes)->setConnection(
$this->query->getConnection()->getName()
);
}
The make method make use of that $pendingAttributes property, so we can configure predefined attributes for a new model instance. The make method is a more superior way to generate models.
I also don't agree with that Larastan's statement.
The make method make use of that $pendingAttributes property, so we can configure predefined attributes for a new model instance. The make method is a more superior way to generate models.
That's not what the property is for. It's used by the Eloquent builder. If you want default values, you use the $attributes property. And if you want custom values, you pass them in the constructor like they did.
Calling make() is unnecessary if you just want a model instance. So I agree with Larastan.