php - 不能在 Laravel 中使用 stdClass 类型的对象作为数组
问题描述
我收到以下错误:
PHP 致命错误:不能在 C:\xampp\htdocs\enginepoker2\storage\framework\views\10cbbd076bede57a96e59c43af0e1b9e022b4a69.php 中使用 stdClass 类型的对象作为数组,第 115 行
查询生成器:
$statsMoneyInPlay = DB::table('enginepoker_log.poker')
->select(
DB::raw("UNIX_TIMESTAMP(Date(ts)*100) as timestamp"),
DB::raw("SUM(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay")
)
->groupBy(DB::raw("DATE(ts)"))
->orderByRaw("DATE(ts)")
->get()
->toArray();
得到错误的刀片:
@php
foreach ($statsMoneyInPlay as $key => $value) {
echo "[".$value[0].", ".$value[1]."],";
}
@endphp
解决方案
该数据库查询返回一个Collection
对象stdClass
。它不返回Collection
数组。调用toArray
它Collection
会给你一个stdClass
对象数组。
@foreach ($statsMoneyInPlay as $value)
[ {{ $value->timestamp }}, {{ $value->moneyInPlay }} ],
@endforeach
推荐阅读
- java - Firebase 实时数据库检索最后一个子数据
- json - For循环json谷歌脚本
- javascript - 如何在不知道其模型的情况下使用 sequelize 将 JSON 数组批量插入 mysql 数据库?
- python-3.x - 如何在 groupby 直方图上拟合泊松分布?
- sql - 从另一个 id 匹配的表中插入数据
- javascript - 邮件事件后未显示 PHP Mailer 自定义消息
- c# - HotChocolate 转换数据库中的数据?
- node.js - 从 NodeJs 中的另一个文件调用中间件方法
- laravel - Laravel 8 如何使用带有 UUID 主键的工厂播种?
- heroku - Heroku 不保留嵌套的 CRA node_modules 和构建文件夹