laravel表单验证

表单验证方法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 App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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');
    }
posted @ 2017-09-01 11:42  miyaye  阅读(228)  评论(0编辑  收藏  举报