[英]Cannot use object of type stdClass as array in Laravel
I got the following error: 我收到以下错误:
PHP Fatal error: Cannot use object of type stdClass as array in C:\\xampp\\htdocs\\enginepoker2\\storage\\framework\\views\\10cbbd076bede57a96e59c43af0e1b9e022b4a69.php on line 115 PHP致命错误:无法在第115行的C:\\ xampp \\ htdocs \\ enginepoker2 \\ storage \\ framework \\ views \\ 10cbbd076bede57a96e59c43af0e1b9e022b4a69.php中将stdClass类型的对象用作数组
The query builder: 查询生成器:
$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();
The Blade that gets the error: 出现错误的刀片:
@php
foreach ($statsMoneyInPlay as $key => $value) {
echo "[".$value[0].", ".$value[1]."],";
}
@endphp
That DB query returns a Collection
of stdClass
objects. 该数据库查询返回stdClass
对象的Collection
。 It does not return a Collection
of arrays. 它不返回数组的Collection
。 Calling toArray
on that Collection
will give you an array of stdClass
objects. 在该Collection
上调用toArray
将为您提供stdClass
对象的数组。
@foreach ($statsMoneyInPlay as $value)
[ {{ $value->timestamp }}, {{ $value->moneyInPlay }} ],
@endforeach
The error is born from a misuse of the array itself. 该错误源于对数组本身的滥用。 Try the following: 请尝试以下操作:
foreach($statsMoneyInPlay as $stat) {
echo "[" . $stat['timestamp'] . ", " . $stat['moneyInPlay'] . "]";
}
EDIT : The $statsMoneyInPlay
is an array of objects, so the proper way to access it would be: 编辑 : $statsMoneyInPlay
是对象数组,因此访问它的正确方法是:
foreach($statsMoneyInPlay as $stat) {
echo "[" . $stat->timestamp . ", " . $stat->moneyInPlay . "]";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.