我是 Laravel 的初学者。我想根据用户角色限制用户会话/登录。所以我在我的登录控制器中有这个方法。
我有两个角色
基本的
夫妻
public function authenticated(Request $request, User $user)
{
if(Auth::check()) { //check if the user is logged in or not
$user = Auth::user();
if ($user->isBasic()) {
$previous_session = $user->session_id;
if ($previous_session) {
\Session::getHandler()->destroy($previous_session);
}
Auth::user()->session_id = \Session::getId();
Auth::user()->save();
return redirect(route('home'));
} elseif ($user->isCouple()) { //check if the user is logged in or not
$previous_session = $user->session_id;
$login = $user->no_of_logins;
if ($previous_session) {
if($login > 2) {
\Session::getHandler()->destroy($previous_session);
Auth::user()->no_of_logins = $user->decrement('no_of_logins');
}
}
Auth::user()->session_id = \Session::getId();
Auth::user()->no_of_logins = $user->increment('no_of_logins');
Auth::user()->save();
return redirect(route('home'));
}
}
}
}
elseif 语句对我不起作用。我不知道该怎么做。
在我的桌子上,我有这个
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->enum('role', ['subscriber', 'admin', 'basic', 'couple', 'family'])->default('subscriber');
$table->timestamp('email_verified_at')->nullable();
$table->string('avatar')->nullable();
$table->integer('no_of_logins')->default(0);
$table->string('session_id')->nullable();
$table->string('password')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
POPMUISE