Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mattez's avatar

How do activate and deactivate a role?

I need your help here! I want to deactivate a role in my Laravel 6 project. Currently, every role is active by default. I have added a Status column on my roles page to show whether a role is active or inactive. I am not supposed to see the view button if the logged-in user is not a super Admin. Even if I manually set a role to inactive, I am still able to see the view button, how can I deactivate the role, please? I already have a property status whose value determines whether the role is active or not. How do I change or update the value of the name of a role?

@extends('Layouts.app')

@section('content')
    <h1>List of Roles</h1>
    <button type="button" class="btn btn-outline-success">
        <a href="/roles/create">Create New Role</a>
    </button>
    <table class="table">
        <thead>
            <tr>
            <th scope="col">#</th>
            <th scope="col">Name</th>
            <th scope="col">Permission</th>
            <th scope="col">Action</th>
            <th scope="col">Status</th>
            </tr>
        </thead>
        <tbody>
            @foreach($roles as $role)
            
                <tr>
                    <th scope="row">{{ $loop->index + 1 }}</th>
                    <td>
                        {{ $role->name }}
                    </td>
                    <td>
                        @foreach($role->permissions as $permission)
                        {{ $permission->name }}
                        @endforeach
                    </td>
                    <td>
                        @can('view', $role)
                        <button type="button" class="btn btn-info">
                            @if($role->status)
                            <a href="/roles/{{ $role->id }}">View</a>
                            @endif
                        </button>
                        @endcan
                    
                        <button type="button" class="btn btn-warning">
                            <a href="/roles/{{ $role->id}}/edit">Update</a>
                        </button>
                    </td>
                    <td>
                        <a href="roles/{{ $role->id }}" class="btn btn-sm btn-{{ $role->status ? 'success' : 'danger'}}">
                            {{ $role->status ? 'active' : '' }}
                        </a>
                    </td>
                </tr>
            
            @endforeach
        </tbody>
    </table>
    <p class="messg">{{ session('messg') }}</p>
@endsection
0 likes
7 replies
tykus's avatar

You are going to need to provide some relevant code if you want help; we have no idea how your project is structured.

Snapey's avatar

Assuming you have a users table and a roles table, then presumably you have a role_user table where users are assigned to roles.

If you want to remove the role from a user, delete the row in the table

If you want no-one to have the role, delete all references to the role in the role_user table

If you want to disable the role for all users but leave the role in the roles table set the disabled state on the role AND delete all references to the role in the role_user table

Otherwise, explain your code.

Mattez's avatar
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddStatusToRolesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('roles', function (Blueprint $table) {
            $table->boolean('status')->default(1);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('roles', function (Blueprint $table) {
            $table->dropColumn('status');
        });
    }
}
Mattez's avatar
<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
   
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Role' => 'App\Policies\RolePolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Gate::define('view-users', function($user) {

            return $user->role->name == 'Super Admin';

        });


        Gate::define('view-roles', function ($user) {
            
            return $user->role->name == 'Super Admin';
                
        });

        Gate::define('view-permissions', function ($user) {
            
            return $user->role->name == 'Super Admin';
                
        });

        Gate::define('view-products', function ($user) {
            
            return $user->role->name == 'Super Admin';
                
        });

        Gate::define('view', function ($user, $role) {
            
            return $user->role->name == 'Super Admin' && $role->active;
                
        });
 
    }

   
}
Mattez's avatar

I have posted my code snippets to enable you to understand what I am trying to achieve. Only the super admin should see the view button when the role is active. How do I check whether the role is active or inactive? I have been stuck for days now. Please help me out. Thank you

Snapey's avatar

@Mattez Your first step is to look in the database and see if the role is active or inactive

change the value in the database and check if your view reacts as expected.

Bear in mind that if you set it as inactive then no one will see it to make it active again

Mattez's avatar

@snapey thank you very much. I have set it manually in my database as inactive and I can't see the view button as expected. What will I do next from the code so I don't need to be changing it from the database? How do I deactivate the role or check whether it is active or inactive now?

Please or to participate in this conversation.