-1

I am new using laravel, and I got this error like this :

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

this is query builder in 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();
Priyanka khullar
  • 509
  • 1
  • 5
  • 25
  • 1
    change it to`->get()->toArray();` – Jigar Shah Oct 01 '18 at 04:25
  • put get() and I got error like this { SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts), sum(pot + p1pot + p2pot + p3pot + p4p' at line 1 (SQL: select 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) order by Date(ts) asc) } – Muhammad Romi Muhtarom Oct 01 '18 at 04:32

2 Answers2

0

First get() collection and then convert it to array (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();
Dhruv Raval
  • 1,535
  • 1
  • 8
  • 15
  • i put get() and I got error like this { SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts), sum(pot + p1pot + p2pot + p3pot + p4p' at line 1 (SQL: select 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)` order by `Date(ts)` asc) } – Muhammad Romi Muhtarom Oct 01 '18 at 04:31
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)")

returns an Eloquent Query Builder instance. it's like MySQL query. It actually Does Not fetch data from the database.

You need to execute that query to fetch data from database.

to do so. call get() function to the 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();
Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28
  • and this is query from native 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()); – Muhammad Romi Muhtarom Oct 01 '18 at 04:43