Laravel will do it for you if you have good relationships
Auth::user()->agent()->create($request->validated());
Laravel will fill the relationship for you
https://laravel.com/docs/6.x/eloquent-relationships#the-create-method
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
So I have not used Model::create(); a lot, I have used to use the new Model(); but it takes a lot of code and if I can just do it in Model::create(); I don't see the problem. Now my question is this.
I have a simple Agent model, it's a hasMany to User so it has a user_id field.
I think this would work nicely?
StoreAgentRequest.php
public function validationData()
{
return $this->merge([
'user_id' => auth()->user()->id
]);
}
But how do I use the create now?
Agent::create($request->validated());
This only takes validated data right? and I don't want to do all here so that it get's feeded stuff it should not have.
What's the best Laravel way here?
Laravel will do it for you if you have good relationships
Auth::user()->agent()->create($request->validated());
Laravel will fill the relationship for you
https://laravel.com/docs/6.x/eloquent-relationships#the-create-method
Please or to participate in this conversation.