Query error
Hi everyone,
I have an error in query and I'm not able to see what I did wrong:
{
return $request->withOrdering($request->withFilters(
$query
->select(DB::raw('users.id as 'id', users.name as 'name', sum(products.price) as 'total''))
->join('orders', 'users.id', '=', 'orders.user_id')
->join('products', 'products.id', '=', 'orders.product_id')
->groupBy('users.id')
));
}' ```
It give me an error: syntax error, unexpected identifier "id", expecting ")"
Simply because you are writing single quotes inside single quotes, try to remove the inside single quotes, or by skipping them using the backslash,
return $request->withOrdering($request->withFilters(
$query
->select(DB::raw('users.id as id, users.name as name, sum(products.price) as total'))
->join('orders', 'users.id', '=', 'orders.user_id')
->join('products', 'products.id', '=', 'orders.product_id')
->groupBy('users.id')
));
@amjad-ah Thank you so much. Works!
@amjad-ah
Also this query gives me the same error:
->from(DB::raw('
(select
users.id 'id',
users.name 'name',
sum(product.price) 'total',
users.deleted_at
from users
inner join orders on users.id = orders.user_id
inner join products on products.id = orders.product_id
group by users.id) users
'))
->select('id', 'name', 'total', 'deleted_at')
@filipbaginski
just like we did before,
->from(DB::raw('
(select
users.id id,
users.name name,
sum(product.price) total,
users.deleted_at
from users
inner join orders on users.id = orders.user_id
inner join products on products.id = orders.product_id
group by users.id) users
'))
->select('id', 'name', 'total', 'deleted_at')
@amjad-ah We :-) Thanks again. Works! The last problem with this query I have is how by default order column 'total' e.g. asc
Fixed:
->select('id', 'name', 'total', 'deleted_at')
->orderBy('total', 'desc')
Please or to participate in this conversation.