Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- User (id, name, department_id)
- Department (id, name)
- +----+-------------+--------------+
- | id | name | department |
- +----+-------------+--------------+
- | 1 | User 1 | 1 |
- +----+-------------+--------------+
- +----+-------------+--------------+
- | id | name | department_id|
- +----+-------------+--------------+
- | 1 | User 1 | Department 1 |
- +----+-------------+--------------+
- User.php
- public function _department() {
- return $this->belongsTo('AppModelsDepartment','id');
- }
- Department.php
- public function _user() {
- return $this->hasMany('AppModelsUser','department_id');
- }
- class UserCrudController extends CrudController
- {
- public function setup()
- {
- /*
- |-----------------------------------------------------------------------
- | CrudPanel Basic Information
- |-----------------------------------------------------------------------
- */
- $this->crud->setModel('AppModelsUser');
- $this->crud->setRoute(config('backpack.base.route_prefix') . '/user');
- $this->crud->setEntityNameStrings('', 'User');
- $this->crud->allowAccess('list');
- /*
- |-----------------------------------------------------------------------
- | CrudPanel Configuration
- |-----------------------------------------------------------------------
- */
- $this->crud->addColumns([
- [
- 'name' => 'id',
- 'label' => "ID",
- 'type' => 'text',
- ],
- [
- 'name' => 'name',
- 'label' => "Name",
- ],
- [
- 'name' => 'department_id',
- 'type' => 'text',
- 'label' => 'Department',
- 'entity' => '_department',
- 'attribute' => 'name',
- 'model' => 'AppModelsDepartment'
- ],
- ]);
- $this->crud->setRequiredFields(StoreRequest::class, 'create');
- $this->crud->setRequiredFields(UpdateRequest::class, 'edit');
- }
- public function store(StoreRequest $request)
- {
- // your additional operations before save here
- $redirect_location = parent::storeCrud($request);
- // your additional operations after save here
- // use $this->data['entry'] or $this->crud->entry
- return $redirect_location;
- }
- public function update(UpdateRequest $request)
- {
- // your additional operations before save here
- $redirect_location = parent::updateCrud($request);
- // your additional operations after save here
- // use $this->data['entry'] or $this->crud->entry
- return $redirect_location;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement