如何从FormRequest类方法中访问当前经过身份验证的用户

Tah*_*afi 3 validation input laravel laravel-5

我有一个用户模型,有两个功能来检查用户的性别.对于特定的表单,我创建了一个FormRequest对象.现在,我需要设置一些特定于用户性别的验证规则,即对于男性用户,存在一组规则,而对于女性用户,则存在另一组规则.

这是我的用户模型:

// app\User.php
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    public function is_male()
    {
        return $this->gender == Gender::male();
    }

    public function is_female()
    {
        return $this->gender == Gender::female();
    }

    public function profile_ok()
    {
        return $this->status == 'OK';
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在FormRequest类中,有一个authorize()方法可以检查用户是否已登录并且可以访问表单,该表单使用Auth::check()方法和Auth::user()->profile_ok()方法(),它可以解决任何错误.但在rules()我尝试通过Auth::user()->is_male()它访问当前用户的方法中引发错误说,

FatalErrorException in ProfileRequest.php line 34:
Class 'app\Http\Requests\Auth' not found
Run Code Online (Sandbox Code Playgroud)

这是我的FormRequest类:

// app\Http\Requests\ProfileRequest.php
class ProfileRequest extends Request {
    public function authorize()
    {
        if ( !Auth::check() )
        {
            return false;
        }
        return Auth::user()->profile_ok();
    }

    public function rules()
    {
        if(Auth::user()->is_male())
        {
            return ['rule1' => 'required',]; //etc
        }
        if(Auth::user()->is_female())
        {
            return ['rule2' => 'required',]; //etc
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?如何从rules()方法中访问当前用户?

Mar*_*ala 6

你可以做到 $this->user()