Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnnyhalford93's avatar

Update price which has related to lasting date_sale

I have three tables.

         Sales - id, name, date_sale
         Product - id, name, price
         (Pivot table) sale_product - id, product_id, sale_id

I know to update price. But, I don't know how to do update price within value of date_sale column.

I want to update price with new value, but when my value of date_sale expired, the new value must be replaced with previous value.

Sale Model

        class Sale extends Model
        {
                 protected $fillable = [
                        'name','date_sale',
                  ];

         public function products()
          {
         return $this->belongsToMany('App\Product','sale_product');
          }

          }

Product Model

    class Product extends Model
    {
      protected $fillable = [
         'name','price',
    ];

   public function sales()
   {
    return $this->belongsToMany('App\Sale','sale_product');
   }

   }

My Controller

  public function addstore(Request $request)
  {

$product_id = $request->get('product');
$price_product = $request->get('price');

$product = Product::find($product_id);

$discount = $product->price / (100/$price_product);

return $product->wherePrice($product->price)->update(['price'=>$discount]);
}
0 likes
2 replies
mvd's avatar

Hi @johnnyhalford93

Why don't you add another 'price' column for the sale price in the pivot 'sale_product ' table? Now you don't have to update the price and you have historical (sale) data.

Please or to participate in this conversation.