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

jararze's avatar

I'm getting array_key_first(): Argument #1 ($array) must be of type array, int given

I have this error when trying to save data from any of my posts...

I created a middleware that check roles

public function handle(Request $request, Closure $next, ...$roles): Response
    {
        if (!in_array($request->user()->role, $roles)) {
            abort('403', 'Acción prohibida, usted no tiene permiso a esta parte del sistema');
//            return redirect('/');
        } else {
//            dd($request);
            return $next($request);
        }
    }

ANd my routes file

Route::middleware(['auth', 'role:admin,agent', 'verified'])->group(function () {
    Route::get('/admin/dashboard', [AdminController::class, 'AdminDashboard'])->name('admin.dashboard');

    Route::get('/admin/allProperties', [PropertyController::class, 'list'])->name('admin.allProperties');
    Route::get('/admin/registerProperties', [PropertyController::class, 'create'])->name('admin.registerProperties');
    Route::post('/admin/registerProperties', [PropertyController::class, 'store']);
    Route::get('/admin/editProperties/{id}', [PropertyController::class, 'EditView'])->name('admin.editProperties');
    Route::post('/admin/editProperties', [PropertyController::class, 'Edit'])->name('admin.edittProperties');
    Route::post('/admin/editPropertiesAmenities', [PropertyController::class, 'EditAmenities'])->name('admin.editPropertiesAmenities');
    Route::post('/admin/edittPropertiesImages', [PropertyController::class, 'EditImages'])->name('admin.edittPropertiesImages');
    Route::post('/admin/edittPropertiesMultipleImages', [PropertyController::class, 'EditMultipleImages'])->name('admin.edittPropertiesMultipleImages');
    Route::post('/admin/destroyPropertiesImages', [PropertyController::class, 'destroyMultipleImages'])->name('admin.destroyPropertiesImages');
    Route::post('/admin/addPropertiesFacility', [PropertyController::class, 'addPropertiesFacility'])->name('admin.addPropertiesFacility');
    Route::post('/admin/deletePropertiesFacility', [PropertyController::class, 'deletePropertiesFacility'])->name('admin.deletePropertiesFacility');


});

And i'm getting this error.

array_key_first(): Argument #1 ($array) must be of type array, int given

And making the error in my middleware file.

Can anybody help me please!!!

0 likes
4 replies
LaryAI's avatar
Level 58

The error message "array_key_first(): Argument #1 ($array) must be of type array, int given" indicates that the argument passed to the array_key_first() function is not an array, but an integer.

In your code, the error is likely occurring in the handle() method of your middleware. Specifically, the line abort('403', 'Acción prohibida, usted no tiene permiso a esta parte del sistema'); is causing the issue.

To fix this error, you need to ensure that the first argument passed to the abort() function is an array, not an integer. The first argument of the abort() function should be the HTTP status code, followed by an array of headers.

Here's the corrected code for your middleware:

public function handle(Request $request, Closure $next, ...$roles): Response
{
    if (!in_array($request->user()->role, $roles)) {
        abort(403, ['Acción prohibida, usted no tiene permiso a esta parte del sistema']);
        // return redirect('/');
    } else {
        // dd($request);
        return $next($request);
    }
}

By passing the HTTP status code as an integer (403) and the error message as an array (['Acción prohibida, usted no tiene permiso a esta parte del sistema']), the error should be resolved.

Remember to clear your cache after making this change by running the following command:

php artisan cache:clear

This should fix the "array_key_first()" error in your middleware.

jararze's avatar

I found that my $request data is arraving as string and not like an array.

jararze's avatar
public function handle(Request $request, Closure $next, ...$roles): Response
    {

        dd($request->user());
        if (!in_array($request->user()->role, $roles)) {
            abort('403', ['Acción prohibida, usted no tiene permiso a esta parte del sistema']);
//            return redirect('/');
        } else {
            if(is_array($request)){
                dd("true");
            }else{
                dd("false");
            }
            return $next($request);
        }
    }

the $request value its suppose to be an array but i'm getting an string insted

twifemmy's avatar

Good day dear Laravel Experts, I just got into laravel few weeks ago and my first website crashed just as I was trying to update a blog post. Please I need someone to help me. i'm willing to share my screen through Teamviewer or FTP for anyone willing to assist me. I'm lost totally. The link above is a screenshot of my site error... nigeriabusinesses.com/ the homepage alobe had refused to load correctly....

Please or to participate in this conversation.