ソースを参照

Merge branch 'dev'

jqh 5 年 前
コミット
95575d7fa8

+ 1 - 2
composer.json

@@ -14,11 +14,10 @@
     "require": {
         "php": ">=7.1.0",
         "symfony/dom-crawler": "~3.1|~4.0",
-        "laravel/framework": "~5.5",
+        "laravel/framework": "~5.5|~6.0",
         "doctrine/dbal": "2.*"
     },
     "require-dev": {
-        "laravel/laravel": "~5.5",
         "symfony/css-selector": "~3.1",
         "fzaninotto/faker": "~1.4",
         "laravel/browser-kit-testing": "~5.1"

+ 2 - 2
resources/views/helpers/scaffold.blade.php

@@ -62,7 +62,7 @@
                                     @foreach($tables as $db => $tb)
                                         <optgroup label="{!! $db !!}">
                                             @foreach($tb as $v)
-                                                <option value="{{$db}}-{{$v}}">{{$v}}</option>
+                                                <option value="{{$db}}|{{$v}}">{{$v}}</option>
                                             @endforeach
                                         </optgroup>
                                     @endforeach
@@ -297,7 +297,7 @@
                 get_tr().remove();
                 return;
             }
-            val = val.split('-');
+            val = val.split('|');
             db = val[0];
             tb = val[1];
 

+ 84 - 0
src/Admin.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin;
 
 use Closure;
 use Dcat\Admin\Exception\Handler;
+use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Models\HasPermissions;
 use Dcat\Admin\Controllers\AuthController;
 use Dcat\Admin\Layout\SectionManager;
@@ -96,6 +97,89 @@ class Admin
         return static::$metaTitle ?: config('admin.title');
     }
 
+    /**
+     * @param $repository
+     * @param Closure|null $callback
+     *
+     * @return Grid
+     */
+    public static function grid($repository = null, \Closure $callback = null)
+    {
+        if ($repository instanceof \Closure) {
+            return new Grid(null, $callback);
+        }
+
+        return new Grid($repository, $callback);
+    }
+
+    /**
+     * @param $repository
+     * @param Closure $callable
+     *
+     * @return Form
+     *
+     */
+    public static function form(Repository $repository, Closure $callable = null)
+    {
+        return new Form($repository, $callable);
+    }
+
+    /**
+     * Build a tree.
+     *
+     * @param $model
+     * @param Closure|null $callable
+     *
+     * @return Tree
+     */
+    public static function tree($model, Closure $callable = null)
+    {
+        return new Tree($model, $callable);
+    }
+
+    /**
+     * Build show page.
+     *
+     * @param $repository
+     * @param mixed $callable
+     *
+     * @return Show
+     */
+    public static function show($id = null, $repository = null, \Closure $callable = null)
+    {
+        switch (func_num_args()) {
+            case 0:
+                $show = new Show();
+
+                break;
+            case 1:
+                $show = new Show(null, $id);
+
+                break;
+            case 2:
+                $show = new Show(null, $repository);
+
+                $show->setId($id);
+                break;
+            case 3:
+                $show = new Show($repository, $callable);
+
+                $show->setId($id);
+        }
+
+        return $show;
+    }
+
+    /**
+     * @param Closure $callable
+     *
+     * @return Content
+     */
+    public static function content(Closure $callable = null)
+    {
+        return new Content($callable);
+    }
+
     /**
      * Get current login user.
      *

+ 2 - 2
src/Controllers/HasResourceActions.php

@@ -13,7 +13,7 @@ trait HasResourceActions
      */
     public function update($id)
     {
-        return $this->form($id)->update($id);
+        return $this->form()->update($id);
     }
 
     /**
@@ -35,6 +35,6 @@ trait HasResourceActions
      */
     public function destroy($id)
     {
-        return $this->form($id)->destroy($id);
+        return $this->form()->destroy($id);
     }
 }

+ 1 - 8
src/Controllers/PermissionController.php

@@ -8,16 +8,9 @@ use Dcat\Admin\Grid;
 use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Layout\Row;
 use Dcat\Admin\MiniGrid;
-use Dcat\Admin\Repositories\EloquentRepository;
 use Dcat\Admin\Show;
 use Dcat\Admin\Tree;
-use Dcat\Admin\Widgets\Checkbox;
-use Dcat\Admin\Widgets\Radio;
-use Dcat\Admin\Widgets\Tab;
-use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Routing\Controller;
-use Illuminate\Support\Facades\Input;
-use Illuminate\Support\Facades\URL;
 use Illuminate\Support\Str;
 
 class PermissionController extends Controller
@@ -33,7 +26,7 @@ class PermissionController extends Controller
      */
     public function index(Content $content)
     {
-        if (Input::get('_mini')) {
+        if (request('_mini')) {
             return $content->body($this->miniGrid());
         }
 

+ 117 - 121
src/Controllers/UserController.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Controllers;
 
+use Dcat\Admin\Admin;
 use Dcat\Admin\Auth\Permission;
 use Dcat\Admin\Models\Repositories\Administrator;
 use Dcat\Admin\Models\Administrator as AdministratorModel;
@@ -65,7 +66,7 @@ class UserController extends Controller
         return $content
             ->header(trans('admin.administrator'))
             ->description(trans('admin.edit'))
-            ->body($this->form($id)->edit($id));
+            ->body($this->form()->edit($id));
     }
 
     /**
@@ -88,52 +89,48 @@ class UserController extends Controller
      */
     protected function grid()
     {
-        $grid = new Grid(new Administrator());
+        return Admin::grid(new Administrator('roles'), function (Grid $grid) {
+            $grid->disableBatchDelete();
+            $grid->disableCreateButton();
 
-        $grid->disableBatchDelete();
-        $grid->disableCreateButton();
+            $grid->id('ID')->bold()->sortable();
+            $grid->username;
+            $grid->name;
+            $grid->roles->pluck('name')->label('primary');
 
-        $grid->model()->with('roles');
-
-        $grid->id('ID')->bold()->sortable();
-        $grid->username;
-        $grid->name;
-        $grid->roles->pluck('name')->label('primary');
-
-        $permissionModel = config('admin.database.permissions_model');
-        $roleModel = config('admin.database.roles_model');
-        $nodes = (new $permissionModel)->allNodes();
-        $grid->permissions->display(function ($v, $column) use (&$nodes, $roleModel) {
-            if (empty($this->roles)) {
-                return;
-            }
-            return $column->tree(function (Grid\Displayers\Tree $tree) use (&$nodes, $roleModel) {
-                $tree->nodes($nodes);
+            $permissionModel = config('admin.database.permissions_model');
+            $roleModel = config('admin.database.roles_model');
+            $nodes = (new $permissionModel)->allNodes();
+            $grid->permissions->display(function ($v, $column) use (&$nodes, $roleModel) {
+                if (empty($this->roles)) {
+                    return;
+                }
+                return $column->tree(function (Grid\Displayers\Tree $tree) use (&$nodes, $roleModel) {
+                    $tree->nodes($nodes);
 
-                foreach (array_column($this->roles, 'slug') as $slug) {
-                    if ($roleModel::isAdministrator($slug)) {
-                        $tree->checkedAll();
+                    foreach (array_column($this->roles, 'slug') as $slug) {
+                        if ($roleModel::isAdministrator($slug)) {
+                            $tree->checkedAll();
+                        }
                     }
-                }
+                });
             });
-        });
 
-        $grid->created_at;
-        $grid->updated_at->sortable();
+            $grid->created_at;
+            $grid->updated_at->sortable();
 
-        $grid->actions(function (Grid\Displayers\Actions $actions) {
-            if ($actions->getKey() == AdministratorModel::DEFAULT_ID) {
-                $actions->disableDelete();
-            }
-        });
+            $grid->actions(function (Grid\Displayers\Actions $actions) {
+                if ($actions->getKey() == AdministratorModel::DEFAULT_ID) {
+                    $actions->disableDelete();
+                }
+            });
 
-        $grid->filter(function (Grid\Filter $filter) {
-            $filter->equal('id');
-            $filter->like('username');
-            $filter->like('name');
+            $grid->filter(function (Grid\Filter $filter) {
+                $filter->equal('id');
+                $filter->like('username');
+                $filter->like('name');
+            });
         });
-
-        return $grid;
     }
 
     /**
@@ -169,58 +166,59 @@ class UserController extends Controller
      */
     protected function detail($id)
     {
-        $show = new Show(new Administrator());
+        return Admin::show($id, new Administrator('roles'), function (Show $show) {
+            $show->id;
+            $show->username;
+            $show->name;
 
-        $show->setId($id);
+            $show->avatar->image();
 
-        $show->id;
-        $show->username;
-        $show->name;
+            $show->newline();
 
-        $show->avatar->image();
+            $show->created_at;
+            $show->updated_at;
 
-        $show->newline();
+            $show->divider();
 
-        $show->created_at;
-        $show->updated_at;
+            $show->roles->width(6)->as(function ($roles) {
+                if (! $roles) return;
 
-        $show->divider();
+                return collect($roles)->pluck('name');
+            })->label('primary');
 
-        $show->roles->width(6)->as(function ($roles) {
-            return collect($roles)->pluck('name');
-        })->label('primary');
+            $show->permissions->width(6)->unescape()->as(function () {
+                $roles = (array) $this->roles;
 
-        $show->permissions->width(6)->unescape()->as(function () {
-            $permissionModel = config('admin.database.permissions_model');
-            $roleModel = config('admin.database.roles_model');
-            $permissionModel = new $permissionModel;
-            $nodes = $permissionModel->allNodes();
+                $permissionModel = config('admin.database.permissions_model');
+                $roleModel = config('admin.database.roles_model');
+                $permissionModel = new $permissionModel;
+                $nodes = $permissionModel->allNodes();
+
+                $tree = Tree::make($nodes);
 
-            $tree = Tree::make($nodes);
+                $isAdministrator = false;
+                foreach (array_column($roles, 'slug') as $slug) {
+                    if ($roleModel::isAdministrator($slug)) {
+                        $tree->checkedAll();
+                        $isAdministrator = true;
+                    }
+                }
 
-            $isAdministrator = false;
-            foreach (array_column($this->roles, 'slug') as $slug) {
-                if ($roleModel::isAdministrator($slug)) {
-                    $tree->checkedAll();
-                    $isAdministrator = true;
+                if (!$isAdministrator) {
+                    $keyName = $permissionModel->getKeyName();
+                    $tree->checked(
+                        $roleModel::getPermissionId(array_column($roles, $keyName))->flatten()
+                    );
                 }
-            }
 
-            if (!$isAdministrator) {
-                $keyName = $permissionModel->getKeyName();
-                $tree->checked(
-                    $roleModel::getPermissionId(array_column($this->roles, $keyName))->flatten()
-                );
+                return $tree->render();
+            });
+
+            if ($show->getId() == AdministratorModel::DEFAULT_ID) {
+                $show->disableDeleteButton();
             }
 
-            return $tree->render();
         });
-
-        if ($id == AdministratorModel::DEFAULT_ID) {
-            $show->disableDeleteButton();
-        }
-
-        return $show;
     }
 
     /**
@@ -228,57 +226,61 @@ class UserController extends Controller
      *
      * @return Form
      */
-    public function form($id = null)
+    public function form()
     {
-        $userTable = config('admin.database.users_table');
+        return Admin::form(new Administrator('roles'), function (Form $form) {
+            $userTable = config('admin.database.users_table');
 
-        $connection = config('admin.database.connection');
+            $connection = config('admin.database.connection');
 
-        $form = new Form(new Administrator());
+            $id = $form->getKey();
 
-        $form->display('id', 'ID');
+            $form->display('id', 'ID');
 
-        $form->text('username', trans('admin.username'))
-            ->required()
-            ->creationRules(['required', "unique:{$connection}.{$userTable}"])
-            ->updateRules(['required', "unique:{$connection}.{$userTable},username,$id"]);
-        $form->text('name', trans('admin.name'))->required();
-        $form->image('avatar', trans('admin.avatar'));
-
-        if ($id) {
-            $form->password('password', trans('admin.password'))
-                ->rules('confirmed')
-                ->customFormat(function ($v) {
-                    if ($v == $this->password) {
-                        return;
-                    }
-                    return $v;
-                });
-            $form->password('password_confirmation', trans('admin.password_confirmation'));
-        } else {
-            $form->password('password', trans('admin.password'))
+            $form->text('username', trans('admin.username'))
                 ->required()
-                ->rules('confirmed');
-
-            $form->password('password_confirmation', trans('admin.password_confirmation'));
-        }
+                ->creationRules(['required', "unique:{$connection}.{$userTable}"])
+                ->updateRules(['required', "unique:{$connection}.{$userTable},username,$id"]);
+            $form->text('name', trans('admin.name'))->required();
+            $form->image('avatar', trans('admin.avatar'));
+
+            if ($id) {
+                $form->password('password', trans('admin.password'))
+                    ->rules('confirmed')
+                    ->customFormat(function ($v) {
+                        if ($v == $this->password) {
+                            return;
+                        }
+                        return $v;
+                    });
+                $form->password('password_confirmation', trans('admin.password_confirmation'));
+            } else {
+                $form->password('password', trans('admin.password'))
+                    ->required()
+                    ->rules('confirmed');
+
+                $form->password('password_confirmation', trans('admin.password_confirmation'));
+            }
 
-        $form->ignore(['password_confirmation']);
+            $form->ignore(['password_confirmation']);
 
-        $form->multipleSelect('roles', trans('admin.roles'))
-            ->options(function () {
-                $roleModel = config('admin.database.roles_model');
+            $form->multipleSelect('roles', trans('admin.roles'))
+                ->options(function () {
+                    $roleModel = config('admin.database.roles_model');
 
-                return $roleModel::all()->pluck('name', 'id');
-            })
-            ->customFormat(function ($v) {
-                return array_column($v, 'id');
-            });
+                    return $roleModel::all()->pluck('name', 'id');
+                })
+                ->customFormat(function ($v) {
+                    return array_column($v, 'id');
+                });
 
-        $form->display('created_at', trans('admin.created_at'));
-        $form->display('updated_at', trans('admin.updated_at'));
+            $form->display('created_at', trans('admin.created_at'));
+            $form->display('updated_at', trans('admin.updated_at'));
 
-        $form->saving(function (Form $form) {
+            if ($id == AdministratorModel::DEFAULT_ID) {
+                $form->disableDeleteButton();
+            }
+        })->saving(function (Form $form) {
             if ($form->password && $form->model()->get('password') != $form->password) {
                 $form->password = bcrypt($form->password);
             }
@@ -287,12 +289,6 @@ class UserController extends Controller
                 $form->deleteInput('password');
             }
         });
-
-        if ($id == AdministratorModel::DEFAULT_ID) {
-            $form->disableDeleteButton();
-        }
-
-        return $form;
     }
 
     /**

+ 53 - 16
src/Form.php

@@ -13,13 +13,11 @@ use Dcat\Admin\Widgets\DialogForm;
 use Illuminate\Contracts\Support\MessageProvider;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Arr;
-use Illuminate\Support\Facades\Input;
 use Illuminate\Support\Fluent;
 use Illuminate\Support\MessageBag;
 use Illuminate\Support\Str;
 use Illuminate\Support\Traits\Macroable;
 use Illuminate\Validation\Validator;
-use Spatie\EloquentSortable\Sortable;
 use Symfony\Component\HttpFoundation\Response;
 use Dcat\Admin\Form\Concerns;
 
@@ -177,6 +175,11 @@ class Form implements Renderable
      */
     protected $repository;
 
+    /**
+     * @var Closure
+     */
+    protected $fieldBuilder;
+
     /**
      * @var bool
      */
@@ -237,7 +240,7 @@ class Form implements Renderable
      *
      * @var array
      */
-    public $rows = [];
+    protected $rows = [];
 
     /**
      * @var bool
@@ -255,15 +258,13 @@ class Form implements Renderable
      * @param Repository $model
      * @param \Closure $callback
      */
-    public function __construct(Repository $repository, Closure $callback = null)
+    public function __construct(Repository $repository, ?Closure $callback = null)
     {
         $this->repository = Admin::createRepository($repository);
 
-        $this->builder = new Builder($this);
+        $this->fieldBuilder = $callback;
 
-        if ($callback instanceof Closure) {
-            $callback($this);
-        }
+        $this->builder = new Builder($this);
 
         $this->isSoftDeletes = $this->repository->isSoftDeletes();
 
@@ -332,6 +333,14 @@ class Form implements Renderable
         return $this->builder->isEditing();
     }
 
+    /**
+     * @return bool
+     */
+    public function isDeleting()
+    {
+        return $this->builder->isDeleting();
+    }
+
     /**
      * @return Fluent
      */
@@ -492,11 +501,14 @@ class Form implements Renderable
     {
         try {
             $this->builder->setResourceId($id);
+            $this->builder->setMode(Builder::MODE_DELETE);
 
             $data = $this->repository->getDataWhenDeleting($this);
 
             $this->setModel(new Fluent($data));
 
+            $this->buildField();
+
             if (($response = $this->callDeleting()) instanceof Response) {
                 return $response;
             }
@@ -533,7 +545,7 @@ class Form implements Renderable
      */
     public function store(?array $data = null, $redirectTo = null)
     {
-        $data = $data ?: Input::all();
+        $data = $data ?: request()->all();
 
         if (($response = $this->callSubmitted())) {
             return $response;
@@ -687,7 +699,6 @@ class Form implements Renderable
 
     }
 
-
     /**
      * Handle update.
      *
@@ -702,11 +713,13 @@ class Form implements Renderable
         $redirectTo = null
     )
     {
-        $data = $data ?: Input::all();
+        $data = $data ?: request()->all();
 
         $this->builder->setResourceId($id);
         $this->builder->setMode(Builder::MODE_EDIT);
 
+        $this->buildField();
+
         if (($response = $this->callSubmitted())) {
             return $response;
         }
@@ -1076,8 +1089,6 @@ class Form implements Renderable
      */
     protected function setFieldValue()
     {
-        $this->setModel(new Fluent($this->repository->edit($this)));
-
         $this->callEditing();
 
         $data = $this->model->toArray();
@@ -1089,6 +1100,34 @@ class Form implements Renderable
         });
     }
 
+    /**
+     * @return void
+     */
+    protected function rendering()
+    {
+        if ($isEditing = $this->isEditing()) {
+            $this->setModel(new Fluent($this->repository->edit($this)));
+        }
+
+        $this->buildField();
+
+        if ($isEditing) {
+            $this->setFieldValue();
+        } else {
+            $this->callCreating();
+        }
+    }
+
+    /**
+     * @return void
+     */
+    protected function buildField()
+    {
+        if ($callback = $this->fieldBuilder) {
+            $callback($this);
+        }
+    }
+
     /**
      * Get validation messages.
      *
@@ -1440,9 +1479,7 @@ class Form implements Renderable
     public function render()
     {
         try {
-            if ($this->isCreating()) {
-                $this->callCreating();
-            }
+            $this->rendering();
 
             $this->callComposing();
 

+ 11 - 0
src/Form/Builder.php

@@ -51,6 +51,7 @@ class Builder
      */
     const MODE_EDIT = 'edit';
     const MODE_CREATE = 'create';
+    const MODE_DELETE = 'delete';
 
     /**
      * Form action mode, could be create|view|edit.
@@ -278,6 +279,16 @@ class Builder
         return $this->isMode(static::MODE_EDIT);
     }
 
+    /**
+     * Check if is deleting resource.
+     *
+     * @return bool
+     */
+    public function isDeleting()
+    {
+        return $this->isMode(static::MODE_DELETE);
+    }
+
     /**
      * Set resource Id.
      *

+ 28 - 14
src/Form/Concerns/HasEvents.php

@@ -11,7 +11,7 @@ trait HasEvents
     /**
      * @var array
      */
-    protected $hooks = [
+    protected $__hooks = [
         'creating'  => [],
         'editing'   => [],
         'submitted' => [],
@@ -26,11 +26,13 @@ trait HasEvents
      *
      * @param Closure $callback
      *
-     * @return void
+     * @return $this
      */
     public function creating(Closure $callback)
     {
-        $this->hooks['creating'][] = $callback;
+        $this->__hooks['creating'][] = $callback;
+        
+        return $this;
     }
 
     /**
@@ -38,11 +40,13 @@ trait HasEvents
      *
      * @param Closure $callback
      *
-     * @return void
+     * @return $this
      */
     public function editing(Closure $callback)
     {
-        $this->hooks['editing'][] = $callback;
+        $this->__hooks['editing'][] = $callback;
+
+        return $this;
     }
 
     /**
@@ -50,11 +54,13 @@ trait HasEvents
      *
      * @param Closure $callback
      *
-     * @return void
+     * @return $this
      */
     public function submitted(Closure $callback)
     {
-        $this->hooks['submitted'][] = $callback;
+        $this->__hooks['submitted'][] = $callback;
+
+        return $this;
     }
 
     /**
@@ -62,11 +68,13 @@ trait HasEvents
      *
      * @param Closure $callback
      *
-     * @return void
+     * @return $this
      */
     public function saving(Closure $callback)
     {
-        $this->hooks['saving'][] = $callback;
+        $this->__hooks['saving'][] = $callback;
+
+        return $this;
     }
 
     /**
@@ -74,11 +82,13 @@ trait HasEvents
      *
      * @param Closure $callback
      *
-     * @return void
+     * @return $this
      */
     public function saved(Closure $callback)
     {
-        $this->hooks['saved'][] = $callback;
+        $this->__hooks['saved'][] = $callback;
+
+        return $this;
     }
 
     /**
@@ -88,7 +98,9 @@ trait HasEvents
      */
     public function deleting(Closure $callback)
     {
-        $this->hooks['deleting'][] = $callback;
+        $this->__hooks['deleting'][] = $callback;
+
+        return $this;
     }
 
     /**
@@ -98,7 +110,9 @@ trait HasEvents
      */
     public function deleted(Closure $callback)
     {
-        $this->hooks['deleted'][] = $callback;
+        $this->__hooks['deleted'][] = $callback;
+
+        return $this;
     }
 
     /**
@@ -173,7 +187,7 @@ trait HasEvents
      */
     protected function callListeners($name)
     {
-        foreach ($this->hooks[$name] as $func) {
+        foreach ($this->__hooks[$name] as $func) {
             $this->model && $func->bindTo($this->model);
 
             if (($ret = $func($this)) instanceof Response) {

+ 1 - 2
src/Form/Concerns/HasFiles.php

@@ -3,7 +3,6 @@
 namespace Dcat\Admin\Form\Concerns;
 
 use Symfony\Component\HttpFoundation\File\UploadedFile;
-use Illuminate\Support\Facades\Input;
 use Dcat\Admin\Form\Field;
 
 trait HasFiles
@@ -96,7 +95,7 @@ trait HasFiles
             unset($input['key']);
         }
 
-        Input::replace($input);
+        request()->replace($input);
 
         return $input;
     }

+ 4 - 3
src/Grid.php

@@ -180,10 +180,11 @@ class Grid
     /**
      * Create a new grid instance.
      *
-     * @param Repository $model
-     * @param Closure  $builder
+     * Grid constructor.
+     * @param Repository|null $repository
+     * @param null $builder
      */
-    public function __construct(Repository $repository = null, $builder = null)
+    public function __construct(?Repository $repository = null, ?\Closure $builder = null)
     {
         if ($repository) {
             $this->keyName = $repository->getKeyName();

+ 1 - 2
src/Grid/Concerns/HasExporter.php

@@ -6,7 +6,6 @@ use Dcat\Admin\Grid\Exporter;
 use Dcat\Admin\Grid\Exporters\AbstractExporter;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Grid\Tools;
-use Illuminate\Support\Facades\Input;
 
 trait HasExporter
 {
@@ -111,7 +110,7 @@ trait HasExporter
      */
     public function getExportUrl($scope = 1, $args = null)
     {
-        $input = array_merge(Input::all(), $this->getExporter()->formatExportQuery($scope, $args));
+        $input = array_merge(request()->all(), $this->getExporter()->formatExportQuery($scope, $args));
 
         if ($constraints = $this->model()->getConstraints()) {
             $input = array_merge($input, $constraints);

+ 1 - 2
src/Grid/Filter.php

@@ -33,7 +33,6 @@ use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Str;
 use Illuminate\Support\Collection;
-use Illuminate\Support\Facades\Input;
 
 /**
  * Class Filter.
@@ -392,7 +391,7 @@ class Filter implements Renderable
             return $this->inputs;
         }
 
-        $this->inputs = Arr::dot(Input::all());
+        $this->inputs = Arr::dot(request()->all());
 
         $this->inputs = array_filter($this->inputs, function ($input) {
             return $input !== '' && !is_null($input) && $input !== static::IGNORE_VALUE;

+ 2 - 3
src/Grid/Model.php

@@ -12,7 +12,6 @@ use Illuminate\Pagination\AbstractPaginator;
 use Illuminate\Pagination\LengthAwarePaginator;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Collection;
-use Illuminate\Support\Facades\Input;
 use Illuminate\Support\Facades\Request;
 use Illuminate\Support\Str;
 
@@ -117,7 +116,7 @@ class Model
      *
      * @param Repository $repository
      */
-    public function __construct(Repository $repository = null)
+    public function __construct(?Repository $repository = null)
     {
         if ($repository) {
             $this->repository = Admin::createRepository($repository);
@@ -561,7 +560,7 @@ class Model
      */
     protected function setSort()
     {
-        $this->sort = Input::get($this->sortName, []);
+        $this->sort = request($this->sortName, []);
 
         if (empty($this->sort['column']) || empty($this->sort['type'])) {
             return;

+ 1 - 2
src/Grid/Tools/Paginator.php

@@ -4,7 +4,6 @@ namespace Dcat\Admin\Grid\Tools;
 
 use Dcat\Admin\Grid;
 use Illuminate\Pagination\LengthAwarePaginator;
-use Illuminate\Support\Facades\Input;
 
 class Paginator extends AbstractTool
 {
@@ -35,7 +34,7 @@ class Paginator extends AbstractTool
         $this->paginator = $this->grid->model()->paginator();
 
         if ($this->paginator instanceof LengthAwarePaginator) {
-            $this->paginator->appends(Input::all());
+            $this->paginator->appends(request()->all());
         }
     }
 

+ 2 - 2
src/Models/Repositories/Administrator.php

@@ -9,11 +9,11 @@ use Illuminate\Pagination\AbstractPaginator;
 
 class Administrator extends EloquentRepository
 {
-    public function __construct()
+    public function __construct($relations = [])
     {
         $this->eloquentClass = config('admin.database.users_model');
 
-        parent::__construct();
+        parent::__construct($relations);
     }
 
     public function get(Grid\Model $model)

+ 24 - 2
src/Repositories/EloquentRepository.php

@@ -5,6 +5,7 @@ namespace Dcat\Admin\Repositories;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Show;
+use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model as EloquentModel;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Illuminate\Support\Arr;
@@ -26,13 +27,17 @@ abstract class EloquentRepository extends Repository
      */
     protected $model;
 
-    public function __construct()
+    protected $relations;
+
+    public function __construct($relations = [])
     {
         $this->setKeyName($this->eloquent()->getKeyName());
 
         $this->setIsSoftDeletes(
             in_array(SoftDeletes::class, class_uses($this->eloquent()))
         );
+
+        $this->with($relations);
     }
 
     /**
@@ -81,6 +86,19 @@ abstract class EloquentRepository extends Repository
         return ['*'];
     }
 
+    /**
+     * Set the relationships that should be eager loaded.
+     *
+     * @param  mixed  $relations
+     * @return $this
+     */
+    public function with($relations)
+    {
+        $this->relations = (array) $relations;
+
+        return $this;
+    }
+
     /**
      * Get the grid data.
      *
@@ -91,6 +109,10 @@ abstract class EloquentRepository extends Repository
     {
         $eloquent = $this->eloquent();
 
+        if ($this->relations) {
+            $eloquent = $eloquent->with($this->relations);
+        }
+
         $model->getQueries()->unique()->each(function ($query) use (&$eloquent) {
             if ($query['method'] == 'paginate') {
                 $query['arguments'][1] = $this->getGridColumns();
@@ -388,7 +410,7 @@ abstract class EloquentRepository extends Repository
             }
         }
 
-        return array_unique($relations);
+        return array_unique(array_merge($relations, $this->relations));
     }
 
     /**

+ 11 - 0
src/Repositories/Repository.php

@@ -168,6 +168,17 @@ abstract class Repository implements \Dcat\Admin\Contracts\Repository
         return [];
     }
 
+    /**
+     *
+     * @param mixed ...$params
+     *
+     * @return $this
+     */
+    public static function make(...$params)
+    {
+        return new static(...$params);
+    }
+
     /**
      * Register listeners.
      *

+ 3 - 3
src/Scaffold/FormCreator.php

@@ -28,13 +28,13 @@ EOF
 
             if ($field['name'] == $primaryKey) continue;
 
-            $rows[] = "        \$form->text('{$field['name']}');";
+            $rows[] = "            \$form->text('{$field['name']}');";
         }
         if ($timestamps) {
             $rows[] = <<<EOF
         
-        \$form->display('created_at');
-        \$form->display('updated_at');
+            \$form->display('created_at');
+            \$form->display('updated_at');
 EOF;
         }
 

+ 6 - 6
src/Scaffold/GridCreator.php

@@ -24,20 +24,20 @@ trait GridCreator
 
             if ($field['name'] == $primaryKey) continue;
 
-            $rows[] = "        \$grid->{$field['name']};";
+            $rows[] = "            \$grid->{$field['name']};";
         }
 
         if ($timestamps) {
-            $rows[] = "        \$grid->created_at;";
-            $rows[] = "        \$grid->updated_at->sortable();";
+            $rows[] = "            \$grid->created_at;";
+            $rows[] = "            \$grid->updated_at->sortable();";
         }
 
         $rows[] = <<<EOF
         
-        \$grid->filter(function (Grid\Filter \$filter) {
-            \$filter->equal('$primaryKey');
+            \$grid->filter(function (Grid\Filter \$filter) {
+                \$filter->equal('$primaryKey');
         
-        });
+            });
 EOF;
 
 

+ 5 - 5
src/Scaffold/ShowCreator.php

@@ -18,22 +18,22 @@ trait ShowCreator
         $rows = [];
 
         if ($primaryKey) {
-            $rows[] = "        \$show->{$primaryKey};";
+            $rows[] = "            \$show->{$primaryKey};";
         }
 
         foreach ($fields as $k => $field) {
             if (empty($field['name'])) continue;
 
-            $rows[] = "        \$show->{$field['name']};";
+            $rows[] = "            \$show->{$field['name']};";
 
             if ($k === 1 && (count($fields) > 2 || $timestamps)) {
-                $rows[] = "        \$show->divider();";
+                $rows[] = "            \$show->divider();";
             }
         }
 
         if ($timestamps) {
-            $rows[] = "        \$show->created_at;";
-            $rows[] = "        \$show->updated_at;";
+            $rows[] = "            \$show->created_at;";
+            $rows[] = "            \$show->updated_at;";
         }
 
         return trim(implode("\n", $rows));

+ 12 - 20
src/Scaffold/stubs/controller.stub

@@ -9,6 +9,7 @@ use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Show;
+use Dcat\Admin\Admin;
 
 class DummyClass extends Controller
 {
@@ -55,7 +56,7 @@ class DummyClass extends Controller
         return $content
             ->header(admin_trans_label())
             ->description(admin_trans_label('edit'))
-            ->body($this->form($id)->edit($id));
+            ->body($this->form()->edit($id));
     }
 
     /**
@@ -79,11 +80,9 @@ class DummyClass extends Controller
      */
     protected function grid()
     {
-        $grid = new Grid(new DummyModel);
-
-        {grid}
-
-        return $grid;
+        return Admin::grid(new DummyModel, function (Grid $grid) {
+            {grid}
+        });
     }
 
     /**
@@ -94,28 +93,21 @@ class DummyClass extends Controller
      */
     protected function detail($id)
     {
-        $show = new Show(new DummyModel);
-
-        $show->setId($id);
-
-        {show}
-
-        return $show;
+        return Admin::show($id, new DummyModel, function (Show $show) {
+            {show}
+        });
     }
 
     /**
      * Make a form builder.
      *
-     * @param $id
      * @return Form
      */
-    protected function form($id = null)
+    protected function form()
     {
-        $form = new Form(new DummyModel);
-
-        {form}
-
-        return $form;
+        return Admin::form(new DummyModel, function (Form $form) {
+            {form}
+        });
     }
 
 }

+ 8 - 6
src/Show.php

@@ -81,11 +81,13 @@ class Show implements Renderable
      * Show constructor.
      *
      * @param Model $model
-     * @param mixed $builder
+     * @param \Closure $builder
      */
-    public function __construct(Repository $repository, $builder = null)
+    public function __construct(?Repository $repository = null, ?\Closure $builder = null)
     {
-        $this->repository = Admin::createRepository($repository);
+        if ($repository) {
+            $this->repository = Admin::createRepository($repository);
+        }
         $this->builder = $builder;
 
         $this->initPanel();
@@ -142,7 +144,7 @@ class Show implements Renderable
     /**
      * @return Fluent
      */
-    public function getModel()
+    public function model()
     {
         if (!$this->model) {
             $this->setModel(new Fluent($this->repository->detail($this)));
@@ -275,7 +277,7 @@ class Show implements Renderable
      */
     public function all()
     {
-        $fields = array_keys($this->getModel()->toArray());
+        $fields = array_keys($this->model()->toArray());
 
         return $this->fields($fields);
     }
@@ -559,7 +561,7 @@ class Show implements Renderable
     public function render()
     {
         try {
-            $model = $this->getModel();
+            $model = $this->model();
 
             if (is_callable($this->builder)) {
                 call_user_func($this->builder, $this);

+ 1 - 1
src/Show/Field.php

@@ -642,7 +642,7 @@ HTML;
                 }
 
                 $this->value = $callable->call(
-                    $this->parent->getModel(),
+                    $this->parent->model(),
                     $this->value,
                     ...$params
                 );

+ 1 - 1
src/Tree.php

@@ -127,7 +127,7 @@ class Tree implements Renderable
      *
      * @param Model|null $model
      */
-    public function __construct(Model $model = null, \Closure $callback = null)
+    public function __construct(Model $model = null, ?\Closure $callback = null)
     {
         $this->model = $model;
 

+ 13 - 1
src/Widgets/Dump.php

@@ -21,6 +21,8 @@ class Dump extends Widget
      */
     protected $content = '';
 
+    protected $maxWidth;
+
     /**
      * Dump constructor.
      *
@@ -57,6 +59,13 @@ class Dump extends Widget
         return $this;
     }
 
+    public function maxWidth($width)
+    {
+        $this->maxWidth = $width;
+
+        return $this;
+    }
+
     /**
      * @param mixed $content
      * @return array|null
@@ -76,7 +85,10 @@ class Dump extends Widget
 
     public function render()
     {
-        $this->defaultHtmlAttribute('style', 'white-space:pre-wrap');
+        $this->defaultHtmlAttribute(
+            'style',
+            'white-space:pre-wrap;'.($this->maxWidth ? "max-width:{$this->maxWidth};" : '')
+        );
 
         return <<<EOF
 <div style="padding:{$this->padding}"><pre class="dump" {$this->formatHtmlAttributes()}>{$this->content}</pre></div>