-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Make model relationship creates filtered by fillable array #2846
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
Conversation
$instance = $this->related->newInstance(); | ||
|
||
$instance->setRawAttributes(array_merge($attributes, $foreign)); | ||
$instance->fill($attributes); | ||
$instance->setRawAttributes(array_merge($instance->getAttributes(), $foreign)); |
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.
Unless I'm missing something, shouldn't you be able to just call $instance->fill(array_merge($attributes, $foreign));
?
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.
The foreign key will rarely be fillable.
Just left you a comment... Also, you will need to make get unit tests passing to get this merged :) |
@bencorlett I'm currently working on making tests work, I'm not so experienced at them so it may take a while, but will get them working. The point in why didn't I make the |
Looks like it passes tests now, I want to mention the tests were made by @anlutro. Big thanks to him. |
Another point is that |
I just wanted to give a quick +1 to this issue as I've just been chasing code around the framework trying to work out why my set mutators weren't firing. It was due to the relation's This PR should fix these issues, unless there was another reason that it's always been like this? |
Fixed this a tad differently... will be on 4.2. |
Awesome, cheers! |
I would put it on 4.1 but if people are already working around it I don't want to break their work-arounds on a point release. So 4.2. |
Thank you! Really great! |
@taylorotwell great work btw, but what workaround should we use in the meantime? |
Currently, when creating instances in a model relationship, attribute
$fillable = array(...)
of the relationship is ignored. As stated by the comment, it's done so to allow foreign keys to be automatically set. Now it allows us to do this as it fills attributes beforehand withfill()
and then sets merged raw attributes filtered byfill()
with foreign keys, thus, we may now do things likewhich is much more clean than
In the examples above
'foo'
database table column does not exist and because of that we get the SQL error if we don'tfill()
.