调用未定义的方法Illuminate / Database / Query / Builder :: toArray()

时间:2018-10-01 04:23:36

标签: php mysql database laravel laravel-query-builder

我是使用laravel的新手,并且出现了这样的错误:

Call to undefined method Illuminate\Database\Query\Builder::toArray()

这是laravel中的查询生成器

$statsMoneyInPlay = DB::table('enginepoker_log.poker')
                    ->selectRaw("SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts)")
                    ->selectRaw("sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay")
                    ->groupBy("Date(ts)")
                    ->orderBy("Date(ts)")
                    ->toArray();

2 个答案:

答案 0 :(得分:0)

首先获取get()集合,然后将其转换为数组(toArray())

$statsMoneyInPlay = DB::table('enginepoker_log.poker')
                                ->selectRaw("SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts)")
                                ->selectRaw("sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay")
                                ->groupBy("Date(ts)")
                                ->orderBy("Date(ts)")
                                ->get()
                                ->toArray();

答案 1 :(得分:0)

$statsMoneyInPlay = DB::table('enginepoker_log.poker')
    ->selectRaw("SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts)")
    ->selectRaw("sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay")
    ->groupBy("Date(ts)")
    ->orderBy("Date(ts)")

返回一个Eloquent Query Builder instance。就像MySQL查询一样。实际上,它从数据库中获取数据。

您需要执行该查询才能从数据库中获取数据。

这样做。调用get()函数到Eloquent Query Builder instance

$statsMoneyInPlay = DB::table('enginepoker_log.poker')
    ->selectRaw("SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts)")
    ->selectRaw("sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay")
    ->groupBy("Date(ts)")
    ->orderBy("Date(ts)")
    ->get()
    ->toArray();