Laravel - Trying to access array offset of value of type int
Asked Answered
P

5

14

I have an issue when I try to submit post requests to the database. The interesting thing is, that the error only occurs when I use PHP version 7.4.2. In PHP version 7.3.9 everything works fine. I am using Laravel 6.17.

When is Laravel going to fix this issue ?

That's my stack trace, when the error comes up.

Trying to access array offset on value of type int   
in CreateCourseController.php line 176
at HandleExceptions->handleError()
in CreateCourseController.php line 176
at CreateCourseController->createDates()
in CreateCourseController.php line 101
at CreateCourseController->createCourse()
at call_user_func_array()
in Controller.php line 54
at Controller->callAction()
in ControllerDispatcher.php line 45
at ControllerDispatcher->dispatch()
in Route.php line 219
at Route->runController()
in Route.php line 176
at Route->run()
in Router.php line 681
at Router->Illuminate\Routing\{closure}()
in Pipeline.php line 130
at Pipeline->Illuminate\Pipeline\{closure}()
in SubstituteBindings.php line 41
at SubstituteBindings->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in VerifyCsrfToken.php line 76
at VerifyCsrfToken->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in StartSession.php line 56
at StartSession->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in EncryptCookies.php line 66
at EncryptCookies->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in Pipeline.php line 105
at Pipeline->then()
in Router.php line 683
at Router->runRouteWithinStack()
in Router.php line 658
at Router->runRoute()
in Router.php line 624
at Router->dispatchToRoute()
in Router.php line 613
at Router->dispatch()
in Kernel.php line 170
at Kernel->Illuminate\Foundation\Http\{closure}()
in Pipeline.php line 130
at Pipeline->Illuminate\Pipeline\{closure}()
in TransformsRequest.php line 21
at TransformsRequest->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in TransformsRequest.php line 21
at TransformsRequest->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in ValidatePostSize.php line 27
at ValidatePostSize->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in CheckForMaintenanceMode.php line 63
at CheckForMaintenanceMode->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in TrustProxies.php line 57
at TrustProxies->handle()
in Pipeline.php line 171
at Pipeline->Illuminate\Pipeline\{closure}()
in Pipeline.php line 105
at Pipeline->then()
in Kernel.php line 145
at Kernel->sendRequestThroughRouter()
in Kernel.php line 110
at Kernel->handle()
in index.php line 55

Regards

Pharos answered 26/2, 2020 at 11:30 Comment(1)
Post the post request pleasePlethoric
T
9

Similar issue is already listed on Laravel repository

According to PHP-Doc this is a Backwards Incompatible Change in PHP 7.4

Array-style access of non-arrays

bool, int, float or resource as an array (such as $null["key"]) will now generate a notice.

Tourist answered 26/2, 2020 at 11:39 Comment(2)
Thank you, at least I know the reason now. But what's the new way or the the better way to iterate through an array ? I need to iterate through an array of Integers.Pharos
@NickKoldehoff your error stack clearly show it CreateCourseController.php line 176. Check what is written over thereTourist
W
7

composer update worked for my case:

I had a Laravel 5.6 app, with Php 7.1.

Also, I had to change laracasts/generators in composer.json to ^1.0 from dev-master to get composer update going, ike so:

"laracasts/generators": "^1.0"

Started having this issue working back on some old projects, the issue occurs on php artisan migrate right after composer install.

https://github.com/spatie/laravel-permission/issues/1370#issuecomment-676556816

Welcher answered 19/8, 2020 at 17:15 Comment(1)
Also for me worked composer update on Laravel 5.5.49, PHP 7.4.11Rasping
H
2

It is about PHP version. In php 7.4rc, accessing an array by key, sometimes causes an error. Rarely this error occurs on php 7.2. For checking an array has member or not Just use empty($array) function

Hindquarter answered 17/5, 2020 at 14:6 Comment(0)
C
2

You are probably trying accessing a non-array type (probably an object type).

I have encountered the same though but the difference it makes is that one snippet accesses an object while the other accesses a valid array type.

The code below produces the error Trying to access array offset of value of type int

$mymodel = MyModel::where(array(
            'someid' => $somevar
        ))->get();
foreach($mymodel as $data){
     //etc
}

I tried updating it to:

$mymodel = MyModel::where(array(
            'someid' => $somevar
        ))->get()->toArray();
foreach($mymodel as $data){
     //etc
}

But it would be really helpful that you post your code so we can further check.

Cloudless answered 15/7, 2020 at 15:38 Comment(0)
I
2

If you use php 7.4 and a used package has developed to php <7.4 then shows this error.

Change your php version to an old version (example, php 7.4 to 7.3).

I used php 7.4 FPM but fixed version of mPDF worked with php 7.3 and below. I caught this error, after I changed php version to 7.3 and it worked.

I also used Homestead. See my config for php:

sites:
    - map: myapp.test
      to: /home/vagrant/code/myapp/public
      php: "7.3"

Run vagrant reload --provision in CLI. This command applies the new config.

Interdepartmental answered 15/3, 2021 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.