表单验证方法1:
使用validate方法
public function store(Request $request){
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
表单验证方法2:
使用form request
php artisan make:request createArticleRequest
createArticleRequest.php
<?php
namespace AppHttpRequests;
use IlluminateFoundationHttpFormRequest;
class createArticleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|min:3',
'content'=>'required',
'published_at'=>'required'
];
}
}
使用:
public function store(createArticleRequest $request){
/*$inputs = $request->all();
$inputs['published_at'] = Carbon::now();*/
Article::create( array_merge( ['user_id'=>Auth::user()->id ],$request->all() ) );
return redirect('/article');
}