admin:make
來建立:
php artisan admin:make UserController --model=App\User
提醒一下,如果是在Linux下,要寫成這樣:
php artisan admin:make UserController --model=App\\User
--model=App\User
代表新建立的這個控制器是要對 App\User
這個資料模型做增刪改查。\專案\app\Admin\Controllers\UserController.php
,內容如下:
<?php
namespace App\Admin\Controllers;
use App\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
class UserController extends Controller
{
use HasResourceActions;
/**
* Index interface.
*
* @param Content $content
* @return Content
*/
public function index(Content $content)
{
return $content
->header('Index')
->description('description')
->body($this->grid());
}
/**
* Show interface.
*
* @param mixed $id
* @param Content $content
* @return Content
*/
public function show($id, Content $content)
{
return $content
->header('Detail')
->description('description')
->body($this->detail($id));
}
/**
* Edit interface.
*
* @param mixed $id
* @param Content $content
* @return Content
*/
public function edit($id, Content $content)
{
return $content
->header('Edit')
->description('description')
->body($this->form()->edit($id));
}
/**
* Create interface.
*
* @param Content $content
* @return Content
*/
public function create(Content $content)
{
return $content
->header('Create')
->description('description')
->body($this->form());
}
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new User);
$grid->id('Id');
$grid->name('Name');
$grid->email('Email');
$grid->email_verified_at('Email verified at');
$grid->password('Password');
$grid->address('Address');
$grid->tel('Tel');
$grid->remember_token('Remember token');
$grid->created_at('Created at');
$grid->updated_at('Updated at');
return $grid;
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
$show = new Show(User::findOrFail($id));
$show->id('Id');
$show->name('Name');
$show->email('Email');
$show->email_verified_at('Email verified at');
$show->password('Password');
$show->address('Address');
$show->tel('Tel');
$show->remember_token('Remember token');
$show->created_at('Created at');
$show->updated_at('Updated at');
return $show;
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$form = new Form(new User);
$form->text('name', 'Name');
$form->email('email', 'Email');
$form->datetime('email_verified_at', 'Email verified at')->default(date('Y-m-d H:i:s'));
$form->password('password', 'Password');
$form->text('address', 'Address');
$form->text('tel', 'Tel');
$form->text('remember_token', 'Remember token');
return $form;
}
}