码迷,mamicode.com
首页 > 其他好文 > 详细

laravel 控制器验证

时间:2019-09-12 00:20:27      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:nbsp   控制器   ext   max   cad   validate   facade   body   inpu   

路由文件

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 控制器验证

标签:nbsp   控制器   ext   max   cad   validate   facade   body   inpu   

原文地址:https://www.cnblogs.com/zhidongjian/p/11509339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!