使用模型的foreach循环中的参数无效

时间:2017-10-30 07:12:36

标签: laravel-5.4

嘿我正在编写一个代码,我在其中使用了foreach循环,但它给出了无效参数的错误。

我的代码循环是

 public function handle($request, Closure $next)
{
    foreach(Auth::user()->User as $role){
            if($role->role == 'doctor')
            {
                return $next($request);   
            }
        }
        return redirect('');
}

模型是

 <?php

 namespace App;

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

 class User extends Authenticatable
 {
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

}

请告诉我我做错了什么。

1 个答案:

答案 0 :(得分:0)

你不能在Auth:user()上做一个foreach。如果要验证用户的角色,可以获取正在执行请求的用户对象并验证角色,例如:

public function handle($request, Closure $next)
{
    if($request->user()->rol == 'admin'){
        return $next($request);
    }

    return redirect('/something');
}
相关问题