Skip to content

Instantly share code, notes, and snippets.

@nickdavies791
Last active October 1, 2022 19:27
Show Gist options
  • Save nickdavies791/ec58c2815bdd5326b1f09ee7014ae8c2 to your computer and use it in GitHub Desktop.
Save nickdavies791/ec58c2815bdd5326b1f09ee7014ae8c2 to your computer and use it in GitHub Desktop.
Merging the current user ID to the request in Laravel using web middleware
<?php
<?php
namespace App\Http\Middleware;
use Closure;
class MergeUserIdIntoRequest
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$request->merge(['user_id' => auth()->id()]);
return $next($request);
}
}
@nickdavies791
Copy link
Author

Able to always get the authenticated user ID by $request->user_id .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment