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

muhammadirfanalalikhan67891's avatar

Undefined array key admin

Hi, I am facing an issue in listing the register users in the views, I am using middleware to authenticate route based on roles , I am using an AJAX call to get all registered user and then showing it on views. Here is the code snippet of the middle ware

class UserRoleMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next, $role)
    {
        if (auth()->check() && auth()->user()->role == $role){
            return $next($request);
        } 
        return response()->json("Sorry you are not allowed to access the requested resource", 403);
    }
}

In user model. the middleware is configured like this:

protected function role(): Attribute 
    {
        return new Attribute(
            get: fn ($value) => ["admin","customer"][$value],
        ); 
    }

These are my routes

oute::middleware(['auth', 'verified' ,'role:admin'])->prefix('admin/')->group(function(){
    
    Route::get('dashboard', [HomeController::class, 'adminDashboard'])->name('admin.home');
    
    // User Routes
    Route::get('user', [UserController::class, 'index'])->name('admin.view.users');
    Route::get('user/all', [UserController::class, 'fetchUsers'])->name('admin.fetch.users');
}

I don't have enough understanding of middleware as I have recently started learning Laravel, so can anyone help out. The error is like this Undefined array key "admin", which is then pointing towards the user model class in the debug bar.

0 likes
4 replies
kokoshneta's avatar

return ["admin","customer"][$value]

What is this supposed to be? It’s not very legible, but I believe it does the same as this:

$array = array(0 => 'admin', 1 => 'customer');
return $array[$value];

So what is $value, then (i.e., what is in the role column in your database)? Is it supposed to be an integer?

It looks like the actual value is the string admin, which means that the accessor is looking for $array['admin'], which doesn’t exist because the array only has the array keys 0 and 1.

But if you already have the string admin (or customer, I guess) in your database, why are you making an accessor at all? Why are you creating an array and then trying to return the same string value from that array that you started out with?

muhammadirfanalalikhan67891's avatar

The database has values "admin" and "customer" for user role, but all my other routes are working properly via above mentioned implementation so what could be the problem in this case ?. I have only listed few routes in the above implement to keep it simple.

kokoshneta's avatar

@muhammadirfanalalikhan67891 What “all other routes” do you mean? Do they have the UserRoleMiddleware as well? Because if the database has admin and customer, then there is no way that middleware will ever work. The accessor will always throw an exception.

Let’s say this user’s role is admin in the database. Your accessor will do the equivalent of this:

public function role() {
	$array = [
		0 => 'admin',
		1 => 'customer'
	];

	return $array['admin'];
}

Do you see why that doesn’t work? The string admin is an array value, not an array key, so $array['admin'] will never exist.

But as I asked you before: what is the purpose of the accessor? What’s it supposed to do?

muhammadirfanalalikhan67891's avatar

From the above answer, I can see that I don't have even an fine understanding of middleware, my implementation is just a follow up of an implementation of a tutorial, but I modified it a bit by using string values in order to accomated more user role in the future, as using boolean values allows only 2 roles (e.g. admin and customer). So I can now understand that part but can you guide me on how to accomodate more user roles using middlware or something else based on custom authentication not on Laravel existing authentication scaffolding ? and if using middleware, could you provide some links. Thanks for your help

Please or to participate in this conversation.