Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

553
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda