前段时间,当我开始使用表单请求类进行数据验证时,我遇到了类似的问题。我注意到以下几点:
如果您使用表单请求来验证数据,则首先检查是否正确设置了允许其通过的授权规则。这由必须返回 的方法处理,默认情况下,该方法设置为 :authorize()
boolean
false
namespace App\Http\Requests\Users;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
/**
* By default it returns false, change it to
* something like this if u are checking authentication
*/
return Auth::check(); // <------------------
/**
* You could also use something more granular, like
* a policy rule or an admin validation like this:
* return auth()->user()->isAdmin();
*
* Or just return true if you handle the authorization
* anywhere else:
* return true;
*/
}
public function rules()
{
// your validations...
}
}