:::

8-1 建立管理買家的後台控制器

  1. Laravel-Admin 的控制器建立方式與普通的控制器建立方式不太一樣,要用 admin:make 來建立:
    php artisan admin:make UserController --model=App\User

    提醒一下,如果是在Linux下,要寫成這樣:

    php artisan admin:make UserController --model=App\\User

     

  2. 其中 --model=App\User 代表新建立的這個控制器是要對 App\User 這個資料模型做增刪改查。
  3. 上面指令會自動建立出後台的控制器:\專案\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;
        }
    }
    

到GitHub觀看此單元程式異動


:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D43%26tbdsn%3D1403

計數器

今天: 3618361836183618
昨天: 4096409640964096
總計: 7457146745714674571467457146745714674571467457146