-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[12.x] Fillable BelongsTo relation on Models #55643
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
[12.x] Fillable BelongsTo relation on Models #55643
Conversation
Personally, I would be against this PR - even I like the idea/shortcut. From your example: So, maybe it would be better to convert models to the primary key of the BelongsTo columns ( $car = Car::create([
'name' => 'Lamborghini Aventador',
'driver_id' => $driver,
]) |
Maybe, this can be made a little more transparent by changing |
* @param \Illuminate\Database\Eloquent\Model $instance | ||
* @return $this | ||
*/ | ||
protected function fillBelongsToRelation($relation, $instance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this method is necessary. Are we expecting a user to override this?
The implementation is already very declarative, even if inlined on the call site.
Couldn't this be a custom cast? Besides being opt-in, it would be more explicit than a developer remembering to match all criteria (add to Also, as a custom cast, one could check if they are assigning a model or a value, and act accordingly, either by associating the model, or by just assigning the value to the foreign key attribute. |
Instead of a custom cast it could be just a The idea is to make filling up a I guess the problem here is more about how to communicate to the developer that the attribute to fill is not a column but a relation, and how it can fill it effortlessly in one sentence. The |
03e7fd9
to
ac9f10b
Compare
Okay, made a rework so it will only work on An alternative would have been to create new methods for Would have that been more better than this approach? Let me now. |
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
What?
This PR allows the developer to fill a
BelongsTo
relationship through thefill()
method, instantly creating the related model if necessary. It only needs a Model instance¹, and setting$fillBelongsToRelations
static property totrue
on the model class.How?
When the attribute is filled, it will check if the key is fillable as part of the Model fillable attributes. If it is, and the relation's name matches, it will associate the model into the relation. If the model doesn't exists in the database, it will save it before associating it.
¹: The value must be a Model instance. This way it can be associated with
MorphTo
relations.If the value is not a Model, then it will be understood it's a normal attribute and fill it like always.
This is opt-in, meaning, it has to be explicitly enabled by the developer (at least on this version), but with the hope that developers who require to create related records can do it in less lines and less complexity.