添加formbuilder插件:
Composer应用
composer require kris/laravel-form-builder
下载成功
修改配置文件
在config/app.php
‘providers’ => [
KrisLaravelFormBuilderFormBuilderServiceProvider::class,
]
‘aliases’ =>[
'FormBuilder' => KrisLaravelFormBuilderFacadesFormbuilder::class,
]
创建form
Php artisan make:form Forms/名字–fields =“name:text,intro:text”
生成的文件
public function buildForm() { $this ->add('name', 'text', [ 'rules' => 'required|min:5' ]) ->add('lyrics', 'textarea', [ 'rules' => 'max:5000' ]); }
创建一个空的form
Php artisan make:form Forms/名字
public function buildForm() { }
控制器
引用:
use KrisLaravelFormBuilderFormBuilder;
函数:
public function create(FormBuilder $formBuilder){ $form = $formBuilder->create(AppFormPostForm::class,[ 'method' => 'POST', 'url' => route('photo.store') ]); return view('Photo.form',compact('form')); }
视图:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{!! form($form) !!}
</body>
</html>