Laravel Lego ,一個簡單的 CRUD 工具庫!
ok, talk is cheap, show you the code.
下面示例完整程式碼及效果見:http://lego.zhw.in
程式碼庫見:https://github.com/zhwei/lego-demo
主要控制元件如下:
- Filter :列表篩選器
$filter = Lego::filter(Suite::with('street.city'));
$filter->addText('street.city.name', '城市名稱');
$filter->addAutoComplete('street.name', '街道名稱');
$filter->addText('address', '地址');
$filter->addSelect('type', '公寓型別')->values(Suite::listType());
$filter->addSelect('status', '公寓狀態')->values(Suite::listStatus());
$filter->addDateRange('created_at', '建立時間');
- Grid :列表頁
$grid = Lego::grid(Suite::class); // or Lego::grid($filter)
$grid->add('id', '操作')->pipe(function ($id) {
return link_to(route('demo', 'suite') . '?id=' . $id, '編輯');
});
$grid->add('street.city.name', '城市');
$grid->add('street.name', '街道');
$grid->add('address', '地址');
$grid->add('type', '型別');
$grid->add('status', '狀態');
$grid->add('created_at|date', '建立日期');
$grid->addLeftTopButton('新建公寓', route('demo', 'suite'));
$grid->addBatch('批量刪除')
->action(function (Suite $suite) {
$suite->delete();
});
$grid->addBatch('變更狀態')
->form(function (Form $form) {
$form->addSelect('status')->values(Suite::listStatus())->required();
})
->action(function (Suite $suite, Form $form) {
$suite->status = $form->field('status')->getNewValue();
$suite->save();
});
- Form :表單
use App\Suite;
use Illuminate\Support\Facades\Input;
use Lego\Lego;
$street = Suite::findOrNew(Input::get('id'));
$form = Lego::form($street);
$form->addAutoComplete('street.name', '所屬街道');
$form->addText('address', '公寓地址')->unique();
$form->addSelect('type', '公寓型別')->values(Suite::listType());
$form->addSelect('status', '公寓狀態')
->options(array_combine(Suite::listStatus(), Suite::listStatus()));
$form->addTextarea('note', '備註');
$form->required();
// $form->required(['street.name', 'address', ...]);
$form->success(route('demo', 'suite-list'));