Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

555
Vistas
Laravel 8 gates not working with Auth::guard

Hey I am developing a project where I have roles and permissions for users and I am trying to protect my routes with the help of middleware by defining Gates but it's showing me 403| Not Authorized. I can't understand what the actual problem is?

Into Category Controller

public function addcategory(AdminsRole $adminsroles){

   return view('add-category');

}

Into Routes

Route::get('/add-category', [CategoryController::class, 'addcategory'])->middleware('can:add-category')->name('addcategory');

Into AuthServiceProvider.php

$admin = Auth::guard('admin');

Gate::define('add-category', function ($admin, AdminsRole $adminsroles) {

    if($admin->user()->u_type != 'superadmin'){

        $adminRolescount = $adminsroles::where([
            ['admin_id', '=', $admin->user()->id],
            ['module', '=', 'categories'],
            ['add_access', '=', '1'],
        ])->count();
        return $adminRolescount;

    }else{

        return $adminRolescount = 1;

    } 

});
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think what you're looking for can be simplified. The code in your controller and routes file does not need to be adjusted. I would however change your gate definition to the following:

Gate::define('add-category', function ($user = null) {
    
    // Fetch user from custom guard
    $user = Auth::guard('admin')->user();

    // Check if a valid user instance was passed
    if (is_null($user)) {
        return false;
    }

    // Allow super admins to add categories regardless of AdminsRole existence
    if ($user->u_type === 'superadmin') {
        return true;
    }

    // Check if current user has a matching AdminsRole row with add_access permission
    return AdminsRole::where([
        ['admin_id', '=', $user->id],
        ['module', '=', 'categories'],
        ['add_access', '=', '1'],
    ])->exists();

});

Note that a gate always receives a user instance as their first parameter if there is a logged in user, you needn't supply this yourself. Additionally, you can query for the AdminsRole existence directly via the model, using the id of the user instance that is being checked and automatically supplied to the gate.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda