This article mainly shares with you Laravel to implement simulated user login, hoping to help everyone.
//强制用户id位100的人登录Auth::loginUsingId(100);//获取这个登录用户的id;dd(auth()->user()->id);
During testing, I need to test the relevant information of a certain logged-in user. We can force login in the auth provider. In this case, there is no need to write a loginUsingId in a specific method. .
app\Providers\AuthServiceProvider.php
Add the user you want to log in to the boot, then the user will be logged in globally.
public function boot() { auth()->loginUsingId(348); $this->registerPolicies(); }
After the test is completed, just remove that line. If you want to log in somewhere In this method, temporarily change the user login, then use loginUsingId again.
Related recommendations:
Code to use curl to simulate user login under PHP_PHP tutorial
The above is the detailed content of Laravel implements simulated user login. For more information, please follow other related articles on the PHP Chinese website!