在中间件中调用模型函数并在laravel中检查身份验证

时间:2019-02-15 13:42:34

标签: laravel

我有两个模型,Admin.php和Role.php。有三个表admins,角色和数据透视表admins_roles。我的申请中有两个角色,公证人和title_022。我创建了中间件来根据管理员的角色检查管理员,然后将其重定向到预期的url。但是当经过身份验证的管理员登录时,我只能看到空白页面,而不是重定向的仪表板。我的应用程序没有显示任何错误。它只是向我显示空白页。我该如何解决?

公证中间件->

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class Notary
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check() && Auth::user()->role()){
            return $next($request);
        }else{
            return redirect()->back();
        }
    }
}

Officer中间件->

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class Officer
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check() && Auth::user()->role()){
            return $next($request);
        }else{
            return redirect()->back();
        }
    }
}

管理员模型->

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = 'admin';

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function role()
    {
        return $this->belongsToMany('App\Role','admins_roles');
    }
}

角色模型->

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    public function admins()
    {
        return $this->belongsToMany('App\Admin','admins_roles');
    }
}

我正在使用官员中间件的路线->

Route::resource('order', 'BackendController')->middleware('officer');

0 个答案:

没有答案