mercredi 5 juin 2019

How can I add new value to the price column which has related to lasting value of date column?

I have three tables. Sales - id, name, date_sale Product - id, name, price And the 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);

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

    return $product->wherePrice($product->price)->update(['price'=>$p]);
 }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire