Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override the UserCrudController to add extra fields #254

Closed
lambasoft opened this issue Sep 30, 2020 · 4 comments
Closed

Override the UserCrudController to add extra fields #254

lambasoft opened this issue Sep 30, 2020 · 4 comments

Comments

@lambasoft
Copy link

I created a migration that changes name to 'first_name' and 'last_name' in the users table, and I want to update the required fields to the UserCrudController, so I created the following UserCrudController that extends the original:

<?php

namespace App\Http\Controllers\Admin;

use Backpack\PermissionManager\app\Http\Controllers\UserCrudController as UserCrudControllerAlias;
use Illuminate\Http\Request;

class UserCrudController extends UserCrudControllerAlias
{


    public function setupListOperation()
    {
        $this->crud->setColumns([
            [
                'name'  => 'first_name',
                'label' => trans('backpack::permissionmanager.first_name'),
                'type'  => 'text',
            ],
            [
                'name'  => 'last_name',
                'label' => trans('backpack::permissionmanager.last_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
            ],
        ]);

        // Role Filter
        $this->crud->addFilter(
            [
                'name'  => 'role',
                'type'  => 'dropdown',
                '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'  => 'permissions',
                'type'  => 'select2',
                'label' => trans('backpack::permissionmanager.extra_permissions'),
            ],
            config('permission.models.permission')::all()->pluck('name', 'id')->toArray(),
            function ($value) { // if the filter is active
                $this->crud->addClause('whereHas', 'permissions', function ($query) use ($value) {
                    $query->where('permission_id', '=', $value);
                });
            }
        );
    }
}

In my routes/backpack/custom.php I have the following:

Route::group([
    'prefix'     => config('backpack.base.route_prefix', 'admin'),
    'middleware' => [
        config('backpack.base.web_middleware', 'web'),
        config('backpack.base.middleware_key', 'admin'),
    ],
    'namespace'  => 'App\Http\Controllers\Admin',
], function () { // custom admin routes
    Route::crud('user', 'UserCrudController');
}); // this should be the absolute last line of this file

But it's not working. The UserCrudController is not being overriden. It's still using the original one.
Any idea how to override the route to the new controller?

@lambasoft
Copy link
Author

lambasoft commented Sep 30, 2020

Ok I resolved it by adding to the routes\web.php

Route::group([
    'prefix'     => config('backpack.base.route_prefix', 'admin'),
    'middleware' => [
        config('backpack.base.web_middleware', 'web'),
        config('backpack.base.middleware_key', 'admin'),
    ],
    'namespace'  => 'Admin',
], function () { // custom admin routes
    Route::crud('user', 'UserCrudController');
}); // this should be the absolute last line of this file

And removed the
Route::crud('user', 'UserCrudController');
from routes/backpack/custom.php

I believe this is cause by the auto-discover of the package. It's loading the routes/backpack/custom.php after the web.php. I tried tweaking the config/app.php to load BackpackServiceProvider and PermissionManagerServiceProvider before RouteServiceProvider but I did not work.

I'll keep this open in case someone had a better solution.

@lambasoft
Copy link
Author

Another solution I found is to bind the Controllers in my AppServiceProvider:

    $this->app->bind(
        \Backpack\PermissionManager\app\Http\Controllers\UserCrudController::class,
        \App\Http\Controllers\Admin\UserCrudController::class
    );

Again, please let me know if there's a better solution.

@DoDSoftware
Copy link

DoDSoftware commented Sep 30, 2020

This question is probably better suited to Stack Overflow tagged with backpack-for-laravel. That said:

Because the Permission manager is an addon, it has it's own custom route file for overriding the default routes

Inside routes/backpack/permissionmanager.php you should write:




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',   // edit this namespace as needed
    'prefix'     => config('backpack.base.route_prefix', 'admin'),
    'middleware' => ['web', backpack_middleware()],
], function () { 
    // custom admin routes
    Route::crud('user', 'UserCrudController');
});


Now these routes will be loaded instead of the package's default routes.

You can see how this behavior is controlled in the setupRoutes method here https://github.com/Laravel-Backpack/PermissionManager/blob/master/src/PermissionManagerServiceProvider.php

@tabacitu tabacitu added this to Inbox in 🔁 Backpack v4.1.x (Continuous Dev) via automation Oct 15, 2020
@tabacitu
Copy link
Member

@lambasoft , @DoDSoftware on second thought... I actually started liking @lambasoft 's method better 🤣 What do you think, should we make that the recommended method? Please let me know in this other thread - Laravel-Backpack/community-forum#90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

3 participants