Skip to content
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

Possible to create/update via relationship #2

Closed
samuelcasas opened this issue Dec 2, 2016 · 1 comment
Closed

Possible to create/update via relationship #2

samuelcasas opened this issue Dec 2, 2016 · 1 comment
Assignees

Comments

@samuelcasas
Copy link
Contributor

samuelcasas commented Dec 2, 2016

@czim I've seen that is not possible to achieve this:

$input = $request->all();
$this->user->posts()->create($input);

the only way to create/update related models is by injecting directly the user_id to the data on the Post model and accepting the user_id to be mass assignable, the method for creating looks like this:

$input = $request->all();
$input['user_id'] = $this->user->id;

$client = Client::create($input);

there is any workaround to prevent the creating to do this hacks?

Thanks.

@czim
Copy link
Owner

czim commented Dec 4, 2016

Hmm. If I understand you correctly, you want to have a way around fillable guards for your models.

So normally you'd do something like:

$client = new Client($request->all());
$client->user_id = $this->user->id;
$client->save();

Whereas now, since you're using the nested updater, you can't inject the user_id before saving.

An approach where you could ask the nested model updater to make() the model instead of persisting it wouldn't work, since that would break the post-persistence nested relation flow.

So, judging by your PR, your suggested approach would be something like:

$nestedUpdater = new ModelUpdater(YourModelClass::class);

$nestedUpdater->setAttribute('user_id', $this->user->id);
$nestedUpdater->create($request->all());

Correct?

If so, this makes sense. Let me think about it a bit further, and then I'll probably accept the PR.
I might change the name to make it clearer that this is an unguarded, 'unsafe' operation, though.

@czim czim self-assigned this Dec 4, 2016
@czim czim closed this as completed Dec 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants