-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add support for writing default DB values to models $attributes
property
#785
Comments
This issue is on laravel side and it is expected behavior. When you create a new model, it does not query the created record from database, simply because it doesn't know whether it should or not and leaves this decision to the individual developers (via |
I know 🤔 I never pretended otherwise. I'm talking about providing a way to auto-generate them.
Yes, but this doesn't cover the case when you create a new model, have not yet persisted it and want/need to pass it around: the defaults won't match what your database has. That's why Laravel provides the That's why I suggested an optional way to auto-generate them to free the developer of having to manually maintain them 😄 |
I'm closing this because my use case changed/expanded. I was using What changed is that in many cases I've refactored the actual $attributes into a constant on the model because I need this for tests in places where I need to mass-check model attributes so I can access the default values outside of a "live" model, as in: class SomeModel extends …
public const DEFAULT_ATTRIBUTES = [
'foo' => true,
];
protected $attributes = self::DEFAULT_ATTRIBUTES;
}
class CompletelyDifferentCode {
public function foo() {
// access it
… = SomeModel::DEFAULT_ATTRIBUTES
}
} Supporting would be way of the scope for the feature and since I could not benefit from it anymore, even when implemented, I'm closing this. |
Note: this is similar in spirit to #693 but it's something else
Example migration:
Model (simplified phpdoc as would be generated by ide-helper)
When you create a new model and access the
is_visible
attribute, it's null:outputs
By manually adding it to the
$attributes
property, we get default values out of the box:Outcome:
outputs
What are the thoughts about adding optional support for ide-helper to generate the
$attributes
automatically as a result from the introspection?--default-attributes
protected $attributes = [];
if missingThe text was updated successfully, but these errors were encountered: