Home > Blockchain >  How to add field in laravel backpack add user form
How to add field in laravel backpack add user form

Time:03-24

I need to add some fields in the user create page in admin panel. enter image description here

This page includes only name field, I need to add first name and last name field. What is the way...

enter image description here

CodePudding user response:

You need to do the following:

  1. Create your own class for the User CRUD panel:
<?php

namespace App\Http\Controllers\Admin;

use Backpack\PermissionManager\app\Http\Controllers\UserCrudController as BackpackUserCrudController;

class UserCrudController extends BackpackUserCrudController
{
    public function setupListOperation()
    {
        // This takes care to add all fields from the package. If you need some field removed you could use CRUD::removeField
        parent::setupListOperation();

        CRUD::field('first_name')->type('text');
        // Any other fields that you need
    }

    public function setupUpdateOperation()
    {
        // This takes care to add all fields from the package. If you need some field removed you could use CRUD::removeField
        parent::setupUpdateOperation();

        CRUD::field('first_name')->type('text');
        // Any other fields that you need
    }
}
  1. Then tell Backpack to use your class instead of the one from the permission manager package. In file routes/backpack/permissionmanager.php:
<?php

/*
|--------------------------------------------------------------------------
| Backpack\PermissionManager Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are
| handled by the Backpack\PermissionManager package.
|
*/

Route::group([
    'namespace' => 'Backpack\PermissionManager\app\Http\Controllers',
    'prefix' => config('backpack.base.route_prefix', 'admin'),
    'middleware' => ['web', backpack_middleware()],
], function () {
    Route::crud('permission', 'PermissionCrudController');
    Route::crud('role', 'RoleCrudController');
});

Route::group([
    'namespace' => 'App\Http\Controllers\Admin',
    'prefix' => config('backpack.base.route_prefix', 'admin'),
    'middleware' => ['web', backpack_middleware()],
], function () {
    Route::crud('user', 'UserCrudController');
});

The second group is the one that tells the router to register your controller App\Http\Controllers\Admin\UserCrudController instead of the one from the package. The first group takes care to register the permissions and roles controllers from the package, so there is no need to do any extra steps for them.

  • Related