Home > Enterprise >  The new price does not appear
The new price does not appear

Time:01-05

I have two tables: products (which has price_before and discount_id) and discounts (which has discount_value and discount_id) and I want to show the products with the new price.

This is my query in product controller:

public function newPrice(Request $request)
    {
        $newPrice= Product::join('discounts','discounts.discount_id','=','products.discount_id')
        ->where('products.product_id',$request->id)
        ->select(DB::raw('products.*','(products.prict_before * discounts.discount_value/100) as priceAfter'))
        ->get();
        return response()->json($newPrice);
    }

and this is my route:

Route::get('/newPrice/{id}','App\Http\Controllers\ProductController@newPrice');

My problem is: when I test the query in postman, it shows me the information about the product without new price (priceAfter) and I want to return it.

Can you help me please?

CodePudding user response:

You have a syntax error in your SELECT clause. Try the following.

$newPrice = Product::join('discounts', 
    'discounts.discount_id', '=', 'products.discount_id')
    ->where('products.product_id', $request->id)
    ->select(DB::raw('products.*, 
    (products.price_before * discounts.discount_value/100) as priceAfter'))
    ->get();
  • Related