foreach() 参数必须是 array|object 类型,给定 null”,异常:“ErrorException

问题描述

我正在尝试在 laravel 中使用数据表,但得到以下响应:

{
    "message": "foreach() argument must be of type array|object,null given","exception": "ErrorException",

我已经在我的控制器中尝试过这段代码:

$dataLists= DB::table("table_name")
                ->select('column_1','column_2')
                ->get();
return Datatables::of($dataLists)
            ->make(true);

如果我打印 $datalists 它会以下面的格式显示数据

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [column_1] => 386932
                    [column_2] => data name 1
                )

            [1] => stdClass Object
                (
                    [column_1] => 8764358
                    [column_2] => data name 2
                )
        )
)

我的php版本是:8.0.0 Laravel 版本为:8.20.1

有人可以帮我找出我错在哪里吗?

解决方法

只需清除laravel的配置缓存
php artisan config:cache

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...