DolDurma
DolDurma

Reputation: 17380

Eloquent getting Syntax error or access violation error

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

Upvotes: 2

Views: 132

Answers (2)

freelancer
freelancer

Reputation: 1164

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');

Upvotes: 1

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

Upvotes: 0

Related Questions