1. 程式人生 > >laravel5.4 表單提交

laravel5.4 表單提交

ole ace color laravel section ram set submit for

1.路由配置:

Route::post(‘/posts‘, ‘\App\Http\Controllers\PostController@store‘);

2.防止csrf攻擊

@section(‘content‘)
<form class="form-horizontal" action="/posts" method="post" role="form">
    {{csrf_field()}}
    {{--<input type="hidden" name="_token" value="{{csrf_token()}}" />--}}
    <div class
="form-group"> <label for="firstname" class="col-sm-2 control-label">Title</label> <div class="col-sm-10"> <input type="text" name="title" class="form-control" id="firstname" placeholder="請輸入標題"> </div> </div> <div class
="form-group"> <label for="lastname" class="col-sm-2 control-label">Content</label> <div class="col-sm-10"> <input type="text" name="content" class="form-control" id="lastname" placeholder="請輸入內容"> </div> </div> <div class
="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">save</button> </div> </div> </form> @endsection

3.對模型(model)處理

class Post extends Model
{
protected $guarded = []; //不允許註入的字段:空數組表示允許所有
// protected $fillable = [‘title‘, ‘content‘]; // 可以註入的數據字段
}

4.控制器接收數據

 //創建邏輯
    public function store(){

        /**方法一
        $post = new Post();
        $post->title = \request(‘title‘);
        $post->content = \request(‘content‘);
        $post->save();
         */
        /**方法二
         */
        #$params = [‘title‘=>\request(‘title‘), ‘content‘=>\request(‘content‘)];
        #$posts = Post::create($params);
        $posts = Post::create(\request([‘title‘, ‘content‘]));

        dd($posts);
    }

laravel5.4 表單提交