2

in laravel 5 when in try to implementing this sql command which i don't get any error on phpmyadmin :

SELECT shoppings.*, sum(shoppings.ordering_count) 
FROM `shoppings` join products on products.id = shoppings.product_id 
where `shoppings`.`user_ordering_ip` = '127.0.0.1' 

with this query on laravel as:

$userShoppings = \DB::table('shoppings')
    ->join('products', 'shoppings.product_id', '=', 'products.id')
    ->select('*', \DB::raw('sum(shoppings.ordering_count)'))
    ->where('shoppings.user_ordering_ip', "'".request()->ip()."'")
    ->get();

i get this error:

SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of 
GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is 
illegal if there is no GROUP BY clause
DolDurma
  • 15,753
  • 51
  • 198
  • 377

2 Answers2

1

Try with following query, without ' quotes here request()->ip, I doubt you are not calling ip() method here, and also dont use sum method here

$userShoppings = \DB::table('shoppings as s')
    ->join('products as p', 's.product_id', '=', 'p.id')
    ->select('p.*','s.*')
    ->where('s.user_ordering_ip', request()->ip())
    ->get();

$sum_of_ordering_count = $userShoppings->sum('ordering_count');
DolDurma
  • 15,753
  • 51
  • 198
  • 377
freelancer
  • 1,174
  • 5
  • 12
0

I think your query is correct.

You can try with below Query Code.

$userShoppings = \DB::table('shoppings')
    ->join('products', 'shoppings.product_id', '=', 'products.id')
    ->select('shoppings.*', \DB::raw('sum(shoppings.ordering_count)'))
    ->where('shoppings.user_ordering_ip', '=', '127.0.0.1')
    ->get();

Also it seems there is may be error on request()->ip