Laravel:中间件问题

时间:2018-11-29 22:54:53

标签: laravel laravel-5 middleware

我有中间件 UKM ,我希望表Auth::id()中的ukm母鸡可以访问下一个请求。但是不起作用,如果表中的Auth::id()否,则用户可以访问。

public function handle($request, Closure $next)
{
    $query = DB::table('ukm')->where('id_user',Auth::id())->get();

    foreach($query as $key){
        $cek = $key->id_user;
    }

    if ($cek != NULL) {
        return $next($request);
    }

    return redirect('/');
}

2 个答案:

答案 0 :(得分:1)

尝试一下...

public function handle($request, Closure $next)
{
    $query = DB::table('ukm')->where('id_user',Auth::id())->first();
    if ($query != NULL) {
        return $next($request);
    }

        return redirect('/');
}

答案 1 :(得分:0)

怎么样....

public function handle($request, Closure $next)
    {
        $query = DB::table('ukm')->pluck('id_user')->toArray();

          if(in_array( Auth::user()->id, $query )
             {
               return $next($request);
             }

        return redirect('/');
    }