一,修改系统自带的表和页面
- 环境:laravel9+dcat2+php8.2
此处我想修改admin_users表,添加一个park_id 关联到parks表,须要修改管理员页面
使用继承重写的思路,最好不要直接去修改vendor包里面的dcat源码,只需要找到相应位置,然后复制过来,重写即可。
新建一个model
<?phpnamespace App\Models;use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Dcat\Admin\Models\Administrator as AdminUser;
class Administrator extends AdminUser
{public function __construct( array $attributes = [] ) {array_push( $this->fillable, 'park_id' );parent::__construct( $attributes );}//定义模型关联关系public function park(){return $this->belongsTo(Park::class);}
}
新建一个controller
<?phpnamespace App\Admin\Controllers;use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\UserController;
use Dcat\Admin\Http\Repositories\Administrator;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Models\Administrator as AdministratorModel;
use Illuminate\Support\Facades\Log;class AdministratorController extends UserController
{protected function grid(){ //这里使用我定义的return Grid::make(\App\Models\Administrator::with(['roles','park']), function (Grid $grid) {$grid->column('id', 'ID')->sortable();$grid->column('username');$grid->column('name');if (config('admin.permission.enable')) {$grid->column('roles')->pluck('name')->label('primary', 3);$permissionModel = config('admin.database.permissions_model');$roleModel = config('admin.database.roles_model');$nodes = (new $permissionModel())->allNodes();$grid->column('permissions')->if(function () {return ! $this->roles->isEmpty();})->showTreeInDialog(function (Grid\Displayers\DialogTree $tree) use (&$nodes, $roleModel) {$tree->nodes($nodes);foreach (array_column($this->roles->toArray(), 'slug') as $slug) {if ($roleModel::isAdministrator($slug)) {$tree->checkAll();}}})->else()->display('');}$grid->column( 'park.park', '园区' );$grid->column('created_at');$grid->column('updated_at')->sortable();$grid->quickSearch(['id', 'name', 'username']);$grid->showQuickEditButton();$grid->enableDialogCreate();$grid->showColumnSelector();$grid->disableEditButton();$grid->actions(function (Grid\Displayers\Actions $actions) {if ($actions->getKey() == AdministratorModel::DEFAULT_ID) {$actions->disableDelete();}});});}public function form() {$f = parent::form();$f->text( 'park_id' );return $f;}public function update($id){Log::info("这里编辑");return $this->form()->update($id);}}
重新定义路由
$router->resource('auth/users', \App\Admin\Controllers\AdministratorController::class)->middleware(config('admin.route.middleware'));