laravel 控制器验证
路由文件
web.php
Route::any('/post/create', 'PostController@create');
代码文件 PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller{
public function create(Request $request){
if($request->isMethod('post')){
$data = [];
$data['username'] = $request->input('username');
$data['sex'] = $request->input('sex');
$validatedData = $request->validate([
'username' => 'required',
'sex' => 'required',
],[
'required' => ':attribute 为必填项',
],[
'username' => '姓名',
'sex' => '性别',
]);
Db::table('user')->insert($data);
return 'POST 提交数据';
}
return view('post.create');
}
public function store(){
$validated = $request->validate([
'title' => ['required', 'unique:posts', 'max:255'],
'body' => ['required']
]);
}
}