laravel5.4 表单提交

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);
}
上一篇:vue开发移动端项目 过渡动画问题


下一篇:C++多态及其实现原理