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

martinszeltins's avatar

How to include user id in $validated create?

I have this store method right now but the create functionality is kind of long, I would like to reduce it to just one line by passing the $validated to create. But the problem is that it doesn't include the user id. Any ideas how this might be solved?

public function store(Request $request)
{
    $validated = $this->validateThread();

    $thread = Thread::create([
        'user_id' => auth()->id(),
        'title' => $request->title,
        'channel_id' => $request->channel_id,
        'body' => $request->body,
    ]);
}

Would be really nice if this could be just one line.

public function store(Request $request)
{
    $validated = $this->validateThread();

    $thread = Thread::create($validated);
}

But the $validated doesn't include the user_id :(

0 likes
8 replies
Sinnbeck's avatar

Can you show the code for validateThread()?

martinszeltins's avatar

@sinnbeck sure, here it is

public function validateThread()
{
    $validated = $this->validate(request(), [
        'title' => 'required',
        'body' => 'required',
        'channel_id' => 'required|exists:channels,id',
    ]);

    resolve(Spam::class)->detect(request('body'));

    return $validated;
}

and now $validated contains this

array:3 [
  "title" => "Autem quis quia nisi alias."
  "body" => "Eligendi nesciunt error cupiditate esse labore soluta. Nam consequatur aut assumenda ullam eligendi aliquam. Tenetur aliquam occaecati beatae eum voluptas."
  "channel_id" => 1
]
Sinnbeck's avatar
public function validateThread()
{
    $validated = $this->validate(request(), [
        'title' => 'required',
        'body' => 'required',
        'channel_id' => 'required|exists:channels,id',
    ]);

    resolve(Spam::class)->detect(request('body'));

    return $validated + ['user_id' => auth()->id()];
}
thewebartisan7's avatar

If there is relation in eloquent between User and Thread supposely named threads() you could do:


public function store(Request $request)
{
    $thread = $request->user()->threads()->create($this->validateThread());
}

public function validateThread()
{
    $validated = $this->validate(request(), [
        'title' => 'required',
        'body' => 'required',
        'channel_id' => 'required|exists:channels,id',
    ]);

    resolve(Spam::class)->detect(request('body'));

    return $validated;
}

MarianoMoreyra's avatar

Hi @martinzeltin

Well, you could do an array_merge and pass that to your function instead of using the request() internally.

So you'll end up with something like this

public function store(Request $request)
{
    $validated = $this->validateThread(array_merge(
        request(), 
        ['user_id' => auth()->id()]
    ));

    $thread = Thread::create($validated);
}

and then in your function:

public function validateThread($fields)
{
    $validated = $this->validate($fields, [
	'user_id' => 'required',
        'title' => 'required',
        'body' => 'required',
        'channel_id' => 'required|exists:channels,id',
    ]);

    resolve(Spam::class)->detect(request('body'));

    return $validated;
}

Does that make sense?

MarianoMoreyra's avatar

Although I think I like @sinnbeck solution better, as you don't actually need to include the user_id in the validation, just returning it at the end would be enough.

Please or to participate in this conversation.