0

I have on input as amount and also getting the discount price but getting an error in where condition,

Here is my query,

public function getDiscountProducts(Request $request){

        $amount = $request->input('amount');

        $products = DB::table('products')
        ->select(DB::raw('(((mrp - price) * 100) / mrp) AS discount'))
        ->where(DB::raw('(((mrp - price) * 100) / mrp) AS discount'), $amount)
        ->get();

   
        return response()->json([
            'message' => 'All categories Products',
            'code' => 200,
            'data' => $products,
            'status' => 'success'
        ]);
    }

Please check where I am missing,

Error:

"Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS discount is null' at line 1 (SQL: select (((mrp - price) * 100) / mrp) AS discount from products where (((mrp - price) * 100) / mrp) AS discount is null)"

5
  • what error are you getting? and I assume you are trying to find where 'discount' is $amount? Commented Sep 4, 2020 at 8:52
  • Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS discount is null' at line 1 (SQL: select (((mrp - price) * 100) / mrp) AS discount from products where (((mrp - price) * 100) / mrp) AS discount is null) Commented Sep 4, 2020 at 8:53
  • why are you not doing the where on 'discount'? where('discount', ...) ? Commented Sep 4, 2020 at 8:54
  • tried, but getting error, Column not found: 1054 Unknown column 'discount' in 'where clause' (SQL: select (((mrp - price) * 100) / mrp) AS discount from products where discount is null) Commented Sep 4, 2020 at 8:56
  • did you notice that $amount is null Commented Sep 4, 2020 at 9:17

2 Answers 2

1

try to remove AS discount from where condition.

Sign up to request clarification or add additional context in comments.

Comments

0

try changing this line

->where(DB::raw('(((mrp - price) * 100) / mrp) AS discount'), $amount)

to

->where(DB::raw('(((mrp - price) * 100) / mrp)'), $amount)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.