laravel 控制器验证

Posted zhidongjian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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‘]
]);

以上是关于laravel 控制器验证的主要内容,如果未能解决你的问题,请参考以下文章

Laravel,在控制器中处理验证错误

从 laravel 控制器返回验证错误到 vue

Laravel 验证控制器存储中的变量

Laravel 4:将从存储库获得的验证消息传递给控制器

Laravel系统3.3控制器与表单验证

laravel 控制器验证