Laravel - 尝试访问类型为int的值的数组偏移量

13

当我尝试将帖子请求提交到数据库时,出现了一个问题。有趣的是,只有在使用PHP版本7.4.2时才会出现此错误。在PHP版本7.3.9中一切正常。我正在使用Laravel 6.17。

Laravel什么时候修复这个问题?

当错误出现时,这是我的堆栈跟踪。

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

敬礼


请发布POST请求。 - Alexandre Elshobokshy
5个回答

9

Laravel库中已经列出了类似的问题。

根据PHP-Doc,这是PHP 7.4中的向后不兼容更改。

非数组的数组式访问

boolintfloatresource作为一个array(例如$null["key"])将会生成提示。


谢谢,至少我现在知道原因了。但是遍历数组的新方法或更好的方法是什么?我需要遍历一个整数数组。 - NKol
@NickKoldehoff 你的错误堆栈清楚地显示了CreateCourseController.php line 176。检查一下那里写了什么。 - Sehdev

7

composer update 对我的情况有效:

我有一个使用 Laravel 5.6Php 7.1 的应用程序。

此外,我不得不将 composer.json 中的 laracasts/generatorsdev-master 更改为 ^1.0,以使得 composer update 运行起来,像这样:

"laracasts/generators": "^1.0"

在回顾一些旧项目时遇到了这个问题,该问题发生在 composer install 后的 php artisan migrate

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


3
对于我来说,在 Laravel 5.5.49,PHP 7.4.11 上运行 composer update 也起作用。 - Matjaž Železnik

2
如果您使用的是 php 7.4,而某个已使用的软件包仅支持 php <7.4,则会显示此错误。
请将您的 php 版本更改为旧版本(例如,从 php 7.4 更改为 7.3)。
我使用的是 php 7.4 FPM,但 mPDF 的固定版本仅适用于 php 7.3 及以下版本。在我更改了 php 版本到 7.3 后,这个问题得到了解决。
我还使用了 Homestead。请参考我的 php 配置:
sites:
    - map: myapp.test
      to: /home/vagrant/code/myapp/public
      php: "7.3"

在CLI中运行vagrant reload --provision。这个命令会应用新的配置。

2

这是关于 PHP 版本的问题。 在 PHP 7.4rc 中,有时通过键访问数组会导致错误。 在 PHP 7.2 中很少出现此错误。 要检查数组是否具有成员, 只需使用 empty($array) 函数。


2

您可能正在尝试访问非数组类型(可能是对象类型)。

我也遇到了同样的问题,但不同的是一个片段访问对象,而另一个则访问有效的数组类型。

下面的代码会产生错误:尝试访问值类型为int的数组偏移量

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

我尝试将其更新为:

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

但如果您能够发布您的代码,那将非常有帮助,这样我们就可以进一步检查。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接