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

laksh's avatar
Level 1

How to use roles and permissions through gates

i am creating a new laravel project and i want to implement the concept of roles and permissions without using plugin and i trying to use gates but can't figure out how to use gates

0 likes
11 replies
tykus's avatar

What have you actually tried?

laksh's avatar
Level 1

@tykus this is what i use

if (Auth::attempt($userData)){

        if (Auth::user() && Auth::user()->is_admin == 1){
            return redirect('admin');
        }else {
            return redirect('user');
        }
    }else {
        return response("failed!");
    }

and for create and edit view i also do that but i want to use gates for this how can i do that?

laksh's avatar
Level 1

@SilenceBringer if i want that non-admin can't create a new user so how can i write the gate for that?

SilenceBringer's avatar

@laksh like described in docs

    Gate::define('create-user', function (User $user) {
        return $user->is_admin;
    });

and use it like

		if (! Gate::allows('create-user')) {
            abort(403);
        }
laksh's avatar
Level 1

@SilenceBringer But when i trying to define the gate like in docs. it automatically go like that Gate::define('create-user', function(User::class)) what should i do for this?

jaseofspades88's avatar

@laksh asks a question, people provide insightful links to documentation and @laksh chooses to ignore these and asks the same question again... don't be like @laksh

laksh's avatar
Level 1

@jaseofspades88 umm no i tried those documentation links and after trying i got that error which i showing

laksh's avatar
Level 1

I created a policy belong to a model class contacts and i want that admin can't able to create a contact and i use this code for that

public function create(User $user) { return $user->is_admin = 0; }

it gives me error of 403 on both admin and non-admin users how can i resolve that?

Please or to participate in this conversation.