我有两个自定义的守护程序“ customer”和“ adminuser”,它们具有单独的控制器文件-客户的默认loginController和管理员用户的AdminController。对于管理员用户,我为客户提供了CheckRole中间件和来宾中间件。当我以管理员用户身份登录时,它将始终以同一管理员用户身份登录。不同的管理员登录详细信息不起作用。当我单击注销时,它将注销,而下次登录时,它显示此错误“试图获取非对象的属性'id'”。我正在使用Laravel 5.8;我在这种情况下停留了一周。为什么此登录无效?问题出在哪里?我是Laravel的新手。请帮我。预先感谢。
管理员登录表单
@if ($errors->any())
@foreach ($errors->all() as $error)
<div class="alert alert-danger" role="alert">
<strong>Oops!</strong> {{$error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endforeach
@endif
<form action="/adminUserLogin" method="POST">
@csrf
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Username" name="username" id="username">
<div class="input-group-append">
<span class="fa fa-user input-group-text"></span>
</div>
</div>
<div class="input-group mb-3">
<input type="password" class="form-control" placeholder="Password" name="password" id="password">
<div class="input-group-append">
<span class="fa fa-lock input-group-text"></span>
</div>
</div>
<div class="row">
<div class="col-8">
<div class="checkbox icheck">
<label>
<input type="checkbox"> Remember Me
</label>
</div>
</div>
<!-- /.col -->
<div class="col-4">
<button type="submit" class="btn btn-primary btn-block">Sign In</button>
</div>
<!-- /.col -->
</div>
</form>
LoginController.php用于客户登录
class LoginController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('guest:customer')->except('customerlogout');
}
// login for the customer
public function customerLogin(Request $request)
{
$this->validate($request, [
'username' => 'required|min:3',
'password' => 'required|min:6|max:6'
]);
$status = Auth::guard('customer')->attempt([
'email' => $request->username,
'password' => $request->password
],
$request->get('remember'));
if ($status) {
return redirect()->intended('/customerhome');
}
return redirect()->intended('/customerhome');
}
public function customerlogout(Request $request)
{
Auth::guard('customer')->logout();
return redirect('/index');
}
}
用于管理员用户登录的AdminController.php
<?php
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Auth;
class AdminController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('roles:adminuser')->except('adminlogout');
}
public function AdminLogin(Request $request)
{
$this->validate($request, [
'username' => 'required|min:3',
'password' => 'required|min:6|max:6'
]);
$status = Auth::guard('adminuser')->attempt([
'email' => $request->username,
'password' => $request->password,
]);
if ($status) {
return redirect()->intended('/adminhome');
}
return redirect()->back();
}
public function adminlogout(Request $request)
{
Auth::guard('adminuser')->logout();
$request->session()->flush();
return redirect('/admin/login');
}
}
CheckRole中间件
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use App\Models\AdminUser;
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($guard === "adminuser" && Auth::guard($guard)->check()) {
return redirect('/adminhome');
}
if (Auth::guard($guard)->check()) {
return redirect('/admin/login');
}
return $next($request);
}
}
AdminUser模型
public function hasRole()
{
if(Auth::guard('adminuser')->check()){
$admin_user_id = Auth::guard('adminuser')->user()->id;
$role_id = $this->roles()->select('role_name')->where('roles.id', $admin_user_id)->first();
return $role_id;
}
}
请帮助我!预先感谢。