问题
I am new to Laravel. I have included an additional field in the user's table named status using the line of code given in the code part below. I am trying to learn how to redirect the user based on their roles.
I understand that to retrieve an authenticated user I can use $user = Auth::user();
My question is how I can get the status of the authenticated user to be used in a redirectTo
function that I plan to write.
// This is how I have added the user status field:
$table->enum('status',['user', 'admin', 'disabled'])->default('user');
回答1:
In LoginController, remove the following line.
protected $redirectTo = '/home';
Then add a new method redirectTo()
to the LoginController. The redirectTo()
method takes precedence over the redirectTo
property.
public function redirectTo() {
$role = auth()->user()->status;
// Check user status/role
switch ($role) {
case 'admin':
return '/admin';
case 'user':
return '/user';
default:
return '/login';
}
}
来源:https://stackoverflow.com/questions/55442071/how-to-retrieve-role-status-of-authenticated-user