Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NoLAstNamE's avatar

Merged field in $request->validated()

I recently asked the question below but again I'm having a problem so I'll post another thread about it. https://laracasts.com/discuss/channels/laravel/fill-user-id-when-using-request-validated-in-create

How to save the authenticated user in the field user_id if I am using a form request and saving data by $request->validated()?

Code:

public function store(StorePostRequest $request)
{
    Post::query()
            ->create($request->validated());

        return redirect()->route('posts.index')
            ->with('success', 'Post created.');
}

I found I problem.

I use $request->validated() when storing a post, and the user_id is gone. It only merged to the request and not in validated().

Post::query()
            ->create($request->validated());

The user_id is added when I merge it but it's gone when I used $request->validated()

Result when I dd($request)

$request->merge(['user_id' => auth()->user()->id]);
dd($request);

// dd result
array:4 [▼ // app/Http/Controllers/PostController.php:31
  "_token" => "FEcVt5qA3BZsB82HO5wYzlkyDi6aAmDdWV9NLlAL"
  "title" => "test"
  "content" => "test content"
  "user_id" => null
]

Result when I dd($request->validated())

array:3 [▼ // app/Http/Controllers/PostController.php:44
  "title" => "test"
  "content" => "test content"
]
0 likes
11 replies
LaryAI's avatar
Level 58

One solution to this problem is to override the validated method in the form request class to merge the user_id field before returning the validated data. Here's an example:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StorePostRequest extends FormRequest
{
    public function rules()
    {
        return [
            'title' => 'required|string|max:255',
            'content' => 'required|string',
        ];
    }

    public function validated()
    {
        $data = parent::validated();
        $data['user_id'] = auth()->user()->id;
        return $data;
    }
}

In this example, we're extending the FormRequest class and overriding the validated method. We first call the parent validated method to get the validated data, and then we merge the user_id field with the authenticated user's ID. Finally, we return the merged data.

Now, when we call $request->validated() in the controller's store method, the user_id field will be included in the validated data:

public function store(StorePostRequest $request)
{
    Post::query()
        ->create($request->validated());

    return redirect()->route('posts.index')
        ->with('success', 'Post created.');
}
NoLAstNamE's avatar

@LaryAI I tried this, but it's not working. It's not on point. Thanks Lary the #AI!

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

I usually do something like

    Post::create($request->validated() + ['user_id' => auth()->id]);

or (better)

    auth()->user()->posts()->create($request->validated());

and then the user ID is automatically added by Eloquent

2 likes
luis-cy2's avatar

@snapey What if you want to do the same but inside a custom request once data has been validated?

bretto36's avatar

Check if user_id is in the list of rules on the request. If it's not in the Request rules then it won't be returned by validated.... as it wasn't validated

NoLAstNamE's avatar

@bretto36 Yeah I get that. But you wouldn't typically include user_id in a form, right? that could be a security risk since anyone could submit a request with any random user_id and insert data for another user

JussiMannisto's avatar

@NoLAstNamE You don't need to add user_id at all if you create the model "the right way", which is:

$request->user()->posts()->create($request->validated());

You could add it to the list of validated input by overriding the validated() method in your form request, but it's a bad solution. You could also add the user_id manually when creating the model, but the method above is better and cleaner.

Post::create([
	...$request->validated(),
	'user_id' => $request->user()->id,
])
NoLAstNamE's avatar

@JussiMannisto Yeah. If you read SNAPEY's answer. Anyway, this is an old post. it's better to focus on the "new" ones in the Forum

Please or to participate in this conversation.