【问题标题】:Call to undefined method Illuminate/Database/Query/Builder::toArray()调用未定义的方法 Illuminate/Database/Query/Builder::toArray()
【发布时间】:2019-03-06 04:11:15
【问题描述】:

我是使用 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();

【问题讨论】:

  • 改成->get()->toArray();
  • put get() 我得到了这样的错误 { SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 'SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts)、sum(pot + p1pot + p2pot + p3pot + p4p' 附近使用正确的语法(SQL:选择SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts), sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay from enginepoker_log.poker group by Date( ts) 按日期排序 asc) }

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


【解决方案1】:

先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();

【讨论】:

  • 我把 get() 和我得到这样的错误 { SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的“SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts)、sum(pot + p1pot + p2pot + p3pot + p4p”附近使用正确的语法(SQL:选择SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts), sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay from enginepoker_log.pokerDate(ts) 分组Date(ts) asc) }
【解决方案2】:
$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();

【讨论】:

  • 这是来自原生 php 的查询 $statsMoneyInPlay = array(); $sql_query = "SELECT UNIX_TIMESTAMP(Date(ts))*1000 As ts, sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay FROM enginepoker_log.poker WHERE DealerId = ".$session->dealerId." GROUP BY Date(ts) ORDER BY Date(ts) LIMIT 30 "; $resource = Sql::query($sql_query) or die(mysql_error());
猜你喜欢
  • 2023-03-16
  • 2014-04-23
  • 1970-01-01
  • 2020-01-04
  • 2018-10-03
  • 1970-01-01
  • 2019-04-02
  • 2016-11-25
  • 2017-07-19
相关资源
最近更新 更多