fetch(\App\Models\User::class); } public function setup() { $this->crud->setModel(config('backpack.permissionmanager.models.user')); $this->crud->setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users')); $this->crud->setRoute(backpack_url('user')); $this->crud->addClause('where', 'id', '>', '1'); $this->crud->removeAllFilters(); } public function setupListOperation() { Widget::add([ 'type' => 'chart', 'controller' => \App\Http\Controllers\Admin\Charts\WeeklyUsersChartController::class, // OPTIONALS 'class' => 'card mb-2', 'wrapper' => ['class'=> 'col-md-6'] , 'content' => [ 'header' => 'New Users', 'body' => 'This chart should make it obvious how many new users have signed up in the past 7 days.

', ], ]) ->to('after_content'); $this->crud->addColumns([ [ 'name' => 'name', 'label' => trans('backpack::permissionmanager.name'), 'type' => 'text', ], [ 'name' => 'email', 'label' => trans('backpack::permissionmanager.email'), 'type' => 'email', ], [ // n-n relationship (with pivot table) 'label' => trans('backpack::permissionmanager.roles'), // Table column heading 'type' => 'select_multiple', 'name' => 'roles', // the method that defines the relationship in your Model 'entity' => 'roles', // the method that defines the relationship in your Model 'attribute' => 'name', // foreign key attribute that is shown to user 'model' => config('permission.models.role'), // foreign key model ], [ // n-n relationship (with pivot table) 'label' => trans('backpack::permissionmanager.extra_permissions'), // Table column heading 'type' => 'select_multiple', 'name' => 'permissions', // the method that defines the relationship in your Model 'entity' => 'permissions', // the method that defines the relationship in your Model 'attribute' => 'name', // foreign key attribute that is shown to user 'model' => config('permission.models.permission'), // foreign key model ], ]); if (backpack_pro()) { // Role Filter $this->crud->addFilter( [ 'name' => 'role', 'type' => 'select2', 'label' => trans('backpack::permissionmanager.role'), ], config('permission.models.role')::all()->pluck('name', 'id')->toArray(), function ($value) { // if the filter is active $this->crud->addClause('whereHas', 'roles', function ($query) use ($value) { $query->where('role_id', '=', $value); }); } ); // Extra Permission Filter // $this->crud->addFilter( // [ // 'name' => 'permission', // 'type' => 'select2_multiple', // 'label' => trans('backpack::permissionmanager.extra_permissions'), // ], // config('permission.models.permission')::all()->pluck('name', 'id')->toArray(), // function ($values) { // if the filter is active // //CRUD::addClause('whereIn', 'status', json_decode($values)); // $this->crud->addClause('whereHas', 'permissions', function ($query) use ($value) { // $query->where('permission_id', 'in', json_decode($values)); // }); // } // ); CRUD::filter('pw') ->label(trans('backpack::permissionmanager.extra_permissions')) ->type('select2_multiple') ->values(function () { return config('permission.models.permission')::all()->pluck('name', 'id')->toArray(); }) ->whenActive(function($values) { //var_dump($values); // CRUD::addClause('whereIn', 'status', json_decode($values)); $this->crud->addClause('whereHas', 'permissions', function ($query) use ($values) { $query->whereIn('permission_id', json_decode($values)); }); }); CRUD::filter('pp') ->type('select2_ajax') // set options to customize, www.daterangepicker.com/#options ->method('POST') ->values(backpack_url('user/fetch/user')) ->whenActive(function($value) { CRUD::addClause('where', 'id', '=', $value); }); CRUD::filter('active') ->type('simple') ->whenActive(function() { // CRUD::addClause('active'); // apply the "active" eloquent scope }); CRUD::filter('created_at') ->type('date_range') // set options to customize, www.daterangepicker.com/#options ->date_range_options([ 'timePicker' => true // example: enable/disable time picker ]) ->whenActive(function($value) { // $dates = json_decode($value); // CRUD::addClause('where', 'date', '>=', $dates->from); // CRUD::addClause('where', 'date', '<=', $dates->to); }); CRUD::filter('updated_at') ->type('date') ->whenActive(function($value) { // CRUD::addClause('where', 'date', $value); }); CRUD::filter('email') ->type('text') ->whenActive(function($value) { CRUD::addClause('where', 'email', 'LIKE', "%$value%"); }); CRUD::filter('id') ->type('range') ->whenActive(function($value) { $range = json_decode($value); if ($range->from) { CRUD::addClause('where', 'id', '>=', (float) $range->from); } if ($range->to) { CRUD::addClause('where', 'id', '<=', (float) $range->to); } }); } } public function setupCreateOperation() { $this->addUserFields(); $this->crud->setValidation(StoreRequest::class); } public function setupUpdateOperation() { $this->addUserFields(); $this->crud->setValidation(UpdateRequest::class); } public function destroy($id) { CRUD::hasAccessOrFail('delete'); $userId = \Auth::guard(config('backpack.base.guard'))->user()->id; if ($id == $userId) { return \Alert::error('不可刪除自己!'); } return CRUD::delete($id); } public function setupShowOperation() { // automatically add the columns $this->crud->column('name'); $this->crud->column('email'); $this->crud->column([ // two interconnected entities 'label' => trans('backpack::permissionmanager.user_role_permission'), 'field_unique_name' => 'user_role_permission', 'type' => 'checklist_dependency', 'name' => 'roles_permissions', 'subfields' => [ 'primary' => [ 'label' => trans('backpack::permissionmanager.role'), 'name' => 'roles', // the method that defines the relationship in your Model 'entity' => 'roles', // the method that defines the relationship in your Model 'entity_secondary' => 'permissions', // the method that defines the relationship in your Model 'attribute' => 'name', // foreign key attribute that is shown to user 'model' => config('permission.models.role'), // foreign key model ], 'secondary' => [ 'label' => mb_ucfirst(trans('backpack::permissionmanager.permission_singular')), 'name' => 'permissions', // the method that defines the relationship in your Model 'entity' => 'permissions', // the method that defines the relationship in your Model 'entity_primary' => 'roles', // the method that defines the relationship in your Model 'attribute' => 'name', // foreign key attribute that is shown to user 'model' => config('permission.models.permission'), // foreign key model, ], ], ]); $this->crud->column('created_at'); $this->crud->column('updated_at'); } /** * Store a newly created resource in the database. * * @return \Illuminate\Http\RedirectResponse */ public function store() { $this->crud->setRequest($this->crud->validateRequest()); $this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest())); $this->crud->unsetValidation(); // validation has already been run return $this->traitStore(); } /** * Update the specified resource in the database. * * @return \Illuminate\Http\RedirectResponse */ public function update() { $this->crud->setRequest($this->crud->validateRequest()); $this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest())); $this->crud->unsetValidation(); // validation has already been run return $this->traitUpdate(); } /** * Handle password input fields. */ protected function handlePasswordInput($request) { // Remove fields not present on the user. $request->request->remove('password_confirmation'); $request->request->remove('roles_show'); $request->request->remove('permissions_show'); // Encrypt password if specified. if ($request->input('password')) { $request->request->set('password', Hash::make($request->input('password'))); } else { $request->request->remove('password'); } return $request; } protected function addUserFields() { $this->crud->addFields([ [ 'name' => 'name', 'label' => trans('backpack::permissionmanager.name'), 'type' => 'text', ], [ 'name' => 'email', 'label' => trans('backpack::permissionmanager.email'), 'type' => 'email', ], [ 'name' => 'password', 'label' => trans('backpack::permissionmanager.password'), 'type' => 'password', ], [ 'name' => 'password_confirmation', 'label' => trans('backpack::permissionmanager.password_confirmation'), 'type' => 'password', ], [ // two interconnected entities 'label' => trans('backpack::permissionmanager.user_role_permission'), 'field_unique_name' => 'user_role_permission', 'type' => 'checklist_dependency', 'name' => 'roles,permissions', 'subfields' => [ 'primary' => [ 'label' => trans('backpack::permissionmanager.roles'), 'name' => 'roles', // the method that defines the relationship in your Model 'entity' => 'roles', // the method that defines the relationship in your Model 'entity_secondary' => 'permissions', // the method that defines the relationship in your Model 'attribute' => 'name', // foreign key attribute that is shown to user 'model' => config('permission.models.role'), // foreign key model 'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] 'number_columns' => 3, //can be 1,2,3,4,6 ], 'secondary' => [ 'label' => mb_ucfirst(trans('backpack::permissionmanager.permission_plural')), 'name' => 'permissions', // the method that defines the relationship in your Model 'entity' => 'permissions', // the method that defines the relationship in your Model 'entity_primary' => 'roles', // the method that defines the relationship in your Model 'attribute' => 'name', // foreign key attribute that is shown to user 'model' => config('permission.models.permission'), // foreign key model 'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] 'number_columns' => 3, //can be 1,2,3,4,6 ], ], ], ]); } }