Last active
October 1, 2022 19:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Able to always get the authenticated user ID by
$request->user_id
.