Back

Binding user ID in Laravel requests without the headache: Extend FormRequest, override prepareForValidation(), and merge the user()->id automatically. This way, no need to pass user_id in every request manually! 🎉


image
image

2 • •

130

•
In response to @cebozkurt

You could also do this for all requests with middleware:

class WithUserId
{
public function handle(Request $request, Closure $next): Response
{
$request->merge([
'user_id' => $request->input('user_id') ?: $request->user()?->id,
]);

return $next($request);
}
}


This wouldn't override the user id if it was actually set in the request, but would inject `user_id` into the input for each request that passes through it.

• •

270

•
  • No matching results...
  • Searching...

/ 1000