Home > Net >  How to show a column conditionally in backpack for laravel
How to show a column conditionally in backpack for laravel

Time:03-21

I need show a column conditionally, based on user permission in backpack for laravel. enter image description here

Need to show customer and total column only for admin. In documentation I found visibleInShow, But do not know how to use it.

Note: I need to make the column unvisible with header.

CRUD::column('user')->label('Customer');
CRUD::modifyColumn('user', [
           'name' => 'user',
           'type' => 'closure',
        ]);

CodePudding user response:

Actually visibleInShow is used mostly in special cases like "hide a column from the listing, but still export it" or "hide a column from listing but still allow searching on it".

Based on your question this is how I conditionally hide columns:

protected function setupListOperation()
{
    // Other columns setup (skipped)
    
    if (Auth::user()->isAdmin) {
        CRUD::column('user')->label('Customer');
    }

    // More columns setup (skipped)
}

or to put it simple: only add the column when the condition is met. No need to add the column and then add more logic to hide it.

  • Related