Hola
Hola

Reputation: 2233

Raw Query works on phpmyadmin but not in laravel

I have written a raw Query in phpmyadmin which provide me exact answer.but same query when i tried to execute in Laravel..I got Error:

Raw Query:

SELECT customers.customer_name, customers.voucher_number ,
         services.name, customers.status,sum(carts.amount) FROM  services JOIN customers
        on services.id = customers.service_id JOIN carts on customers.id = carts.customer_id          
        GROUP BY customers.id

in Laravel :

$status_report = DB::select("SELECT customers.customer_name, customers.voucher_number ,
         services.name, customers.status,sum(carts.amount) FROM  services JOIN customers
        on services.id = customers.service_id JOIN carts on customers.id = carts.customer_id         
        GROUP BY customers.id");

What could be the possible Error?

I got the following Error :

SQLSTATE[42000]: Syntax error or access violation: 1055 'maitree.customers.customer_name' isn't in GROUP BY (SQL: SELECT customers.customer_name, customers.voucher_number ,
services.name, customers.status,sum(carts.amount) FROM services JOIN customers
on services.id = customers.service_id JOIN carts on customers.id = carts.customer_id 
GROUP BY services.id)

Upvotes: 4

Views: 712

Answers (2)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21691

It might be a with SQL_MODE, Please makes changes into connection "config/database.php" :

strict => false

Hope this will help you.

Upvotes: 1

Ohgodwhy
Ohgodwhy

Reputation: 50798

You need to use DB::raw to wrap your raw SQL.

$status_report = DB::select(DB::raw("SELECT customers.customer_name, customers.voucher_number ,
     services.name, customers.status,sum(carts.amount) FROM  services JOIN customers
    on services.id = customers.service_id JOIN carts on customers.id = carts.customer_id         
    GROUP BY customers.id"));

Upvotes: 0

Related Questions