我想验证POST方法中的输入,但结果消息显示“不允许使用方法”。这是我的数据库中创建新用户的代码( UserController.php )
public function userRegister(Request $request)
{
$data['error']['state'] = false;
$rules = [
'name' => 'required',
'username' => 'required|unique:username',
'nip' => 'required|unique:nip',
'email' => 'required|unique:email',
'phone' => 'required',
'avatar' => 'required',
'password' => 'required',
'faculty_id' => 'required',
'building_id' => 'required',
'room_id' => 'required'
];
$message = [
'required' => 'Fill the required field.',
'username.unique' => 'Username already taken.',
'nip.unique' => 'Staff ID already taken.',
'email.unique' => 'Email already taken.',
];
$validator = $this->validate($request,$rules,$message);
if($validator->fails()){
$data['error']['state'] = true;
$data['error']['data'] = $validator->errors()->first();
}
else{
$data['user']['name'] = $request->input('user.name');
$data['user']['surname'] = $request->input('user.surname');
$data['user']['username'] = $request->input('user.username');
$data['user']['nip'] = $request->input('user.nip');
$data['user']['email'] = $request->input('user.email');
$data['user']['password'] = Hash::make($request->input('user.password'));
$data['user']['phone'] = $request->input('user.phone');
$data['user']['level'] = $request->input('user.role');
$data['user']['username_telegram'] = $request->input('user.username_telegram');
$data['user']['user_email_action'] = $request->input('user.user_email_action');
$data['user']['user_telegram_action'] = $request->input('user.user_telegram_action');
$data['user']['faculty_id'] = $request->input('user.faculty');
$data['user']['building_id'] = $request->input('user.building');
$data['user']['room_id'] = $request->input('user.room');
$data['user']['verified'] = 0;
if(!empty($request->input('user.avatar'))){
$data['user']['avatar'] = $request->input('user.username').'-'.$request->input('user.new_avatar');
}
else{
$data['user']['avatar'] = 'default.png';
}
$user_id = DB::table('register')->insertGetId($data['user'],'id');
}
return response()->json($data);
}
以下是消息: Error Message
您知道如何解决吗?谢谢。
答案 0 :(得分:1)
您的问题未显示您的申请路线,但您需要确保表单设置为发布
<form action="/my/url/path" method="post">
并且您的路线设置为“发布”
例如
Route::post('/my/url/path', 'MyController@userRegister');
请注意,如果您不使用Laravel Collective,则需要确保包含CSRF令牌
<form method="POST" action="/my/url/path">
@csrf (laravel 5.6)
{{ csrf_field() }} (previous versions)