BackpackForLaravel – A Weblog of Priyank Maniar https://priyank.rocks Articles on web development & mobile app development Sat, 25 Dec 2021 02:04:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 [Resolved] Scroll issue with the export columns visibility | BackpackForLaravel https://priyank.rocks/resolved-scroll-issue-with-the-export-columns-visibility-backpackforlaravel/ Wed, 22 Dec 2021 10:18:28 +0000 https://priyank.rocks/?p=8711 This solution was tested on BackpackForLaravel Version 4.1.

1
2
3
4
5
6
7
8
9
10
/* File: list.css */

/*------------------------------------------------
    Custom Added
--------------------------------------------------*/


.dt-button-collection {
    overflow-y: scroll;
    max-height: 200px;
}
]]>
Backpack for Laravel roles and permissions for the Users module https://priyank.rocks/backpack-for-laravel-roles-and-permissions-module/ Thu, 27 May 2021 16:10:41 +0000 https://priyank.rocks/?p=7790 All details can be found here about the package.

A thing to note here is the package comes with the roles and permissions manageable out of the box. Check out this part for customizing on the user’s module.

We can use our own UsersCrudController as it is far more likely there will be some customization required. For whatever we require from their setup we can copy from their file.

For example: the following gives the manageable roles fields. Copied from their file as is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$this->crud->addFields([
      [
          'tab' => 'Roles',
        // 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'          => 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
                'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
                'number_columns' => 3, //can be 1,2,3,4,6
            ],
        ],
    ],
]);

Gotcha #1

If you are using Mutator for the password field on the Users model. Be sure to update the code accordingly. Here’s what I mean:

1
2
3
4
5
6
7
8
        // Encrypt password if specified.
        if ($request->input('password')) {
//            $request->request->set('password', Hash::make($request->input('password')));
            // Removed hash because User model has a mutator on the password.
            $request->request->set('password', $request->input('password'));
        } else {
            $request->request->remove('password');
        }

Gotcha #2

When the password is updated user is logged out and if you are using prefix, you are logged in to /login which is not even your admin login page.

First, let’s understand why we are logged out? Read this post.

The backpack version of solution to it is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
     * 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();

        /**
         * Otherwise the use will be logged out when the password is changed!
         * @source https://stackoverflow.com/questions/47147988/prevent-logout-after-updating-user-password-in-laravel-5-5
         */
        Auth::loginUsingId(Request::segment(3));

        return $return;
    }
]]>
All about intercepting the BackpackForLaravel create command https://priyank.rocks/all-about-intercepting-the-backpackforlaravel-create-command/ Tue, 24 Nov 2020 08:28:40 +0000 https://priyank.rocks/?p=7321 How to assign a custom Role to the saved user record with BackPackForLaravel?

I was saving a user and when the user is saved, I wanted to assign a role. Here’s how I achieved it.

I created a store method that overrides the store method in CrudController the store function in my module (CrudController) was a copy of the store method inside the CrudController. I just had to add one line to add the Role by intervening the process.

1
2
3
4
// insert item in the db
$item = $this->crud->create($this->crud->getStrippedSaveRequest());
// THIS LINE
$item->assignRole('Admin');
]]>
How to add conditions to the query/listing module in BackpackForLaravel https://priyank.rocks/how-to-add-conditions-to-the-query-listing-module-in-backpackforlaravel/ Sat, 21 Nov 2020 12:31:07 +0000 https://priyank.rocks/?p=7319 How to query for a specific role in the admin panel using BackpackForLaravel
1
$this->crud->addClause('role', 'Admin');

How to remove ellipsis on a column for BackpackForLaravel?

One might have any type of column such as relationship, but when the column text is long, it gets truncated. This is not done via CSS, so we need a different kind of solution for this.

I encountered this issue for a Relationship field and the easiest fix was to convert the column to a Closure and then the output is not going to be truncated.

Converting a Relationship to a Closure is easy as it can get.

1
2
3
4
5
6
7
8
9
10
11
$this->crud->addColumn(
    [
        'type'     => "closure",
        'name'     => 'relationship_function',  
        'label'    => 'Label',
        'function' => function($entry) {

            return $entry->relationship_function->column;
        },
    ]
);

How to apply a scope?

1
$this->crud->addClause('active'); // apply a local scope
]]>
403 This action is unauthorized | BackpackForLaravel | Using Permission Manager of Spatie https://priyank.rocks/403-this-action-is-unauthorized-backpackforlaravel-using-permission-manager-of-spatie/ Sat, 21 Nov 2020 12:05:46 +0000 https://priyank.rocks/?p=7314 I needed to customize the default user module that we get with the Permission Manager package. The author recommends extending the classes (\Backpack\PermissionManager\app\Http\Controllers\UserCrudController) instead of just mere copy-pasting it and unlinking it from the package code. I still copied the code from the vendor controller files (UserCrudController) and request files into the project’s app folder into their respective folders as a starting point and extended it to the code placed in the vendor directory by the package (\Backpack\PermissionManager\app\Http\Controllers\UserCrudController). The listing worked as expected however the update gave a 403.

Solution

So, my UserCrudController was extending

\Backpack\PermissionManager\app\Http\Controllers\UserCrudController

and the above file was extending CrudController

Not exactly sure why but I realized the $this->crud thing isn’t working as expected. So I removed all the code from my UserCrudController which was a duplicate from \Backpack\PermissionManager\app\Http\Controllers\UserCrudController such as the functions – setup, setupCreateOperation, setupUpdateOperation, except for my field customizations and it worked.

Doing this will also help us have a cleaner UserCrudController

]]>