Answer the question
In order to leave comments, you need to log in
How to set default parameters of an entity?
1 - When creating a migration, fixtures, set the default value.
2 - always set when saving
3 - in the constructor when creating
How is the approach more flexible? In what situations is it worth sculpting the data manually and when to set the default value in the migration?
If we say I set the default value for migration, then this is not the most obvious behavior of the program, new developers may not immediately understand that when creating an entity in the database, something already stands on its own.
On the other hand, if we forget to attach data to a parameter, then the database will do it for us.
Schema::create('users')...
->('name')->default('My default name')
// or
class User {
...
public function __construct(
$name
) {
$this->name = $name;
...
}
Answer the question
In order to leave comments, you need to log in
setting default values is part of the object initialization process and should be the responsibility of the constructor.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question