Doubt when performing SQL with Eloquent of Laravel

3

I have a bug that I do not know the reason for it.

I have in my controller the following Eloquent :

$balances = BankUser::select(DB::raw('SUM(balance) AS total, 
          created_at, DATE(created_at) AS total_at'))
    ->whereYear('created_at', $year)->whereMonth('created_at', $month)
    >groupBy('total_at')->get();

That is generating me the following error:

  

SQLSTATE[42000]: Syntax error or access violation: 1055 'db_develop.account_bank_user.created_at' isn't in GROUP BY (SQL: select SUM(balance) AS total, created_at, DATE(created_at) AS total_at from ut_banks_user where year(created_at) = 2018 and month(created_at) = 12 group by total_at)

But if I copy the SQL being generated:

  

select SUM(balance) AS total, created_at, DATE(created_at) AS total_at from account_bank_user where year( created_at ) = 2018 and month( created_at ) = 12 group by total_at ''

And I run in my PHPMyAdmin it queries and returns the records, what is missing in the code?

    
asked by anonymous 12.12.2018 / 16:46

2 answers

3

According to the answers from this question that I find very interesting , I got one of the simplest answers

$balances = 
  BankUser::select(DB::raw('SUM(balance) AS total,created_at, DATE(created_at) AS total_at'))
                    ->whereYear('created_at', $year)->whereMonth('created_at', $month)
                    ->groupBy(DB::raw('Date(created_at)')->get();

Note the groupBy inside uses DB::raw()

    
12.12.2018 / 17:32
2

To be very pissed but happy to have arranged hahaha, thanks for the tips, but solved in a way so stupid that not yet fell.

$balances = BankUser::select(DB::raw('SUM(balance) AS total, DATE(created_at) AS total_at'))
    ->whereYear('created_at', $year)->whereMonth('created_at', $month)
    ->groupBy('total_at')->get();

That is, I just removed the first select created_at

    
12.12.2018 / 18:51