protected $table = 'products';
protected $fillable=['photo_id','category_id','name','title','description','price','quantity'];
public function category(){
return $this->belongsTo('App\Category');
}
public function photo(){
return $this->belongsTo('App\Photo');
}
/*public function stock(){
return $this->hasMany('App\Stock');
}*/
public function order(){
return $this->belongsToMany('App\Order', 'order_product', 'product_id', 'order_id');
}
Maybe something like the below might work. I think your issue might be that you are performing a
calculation within the parameters of the Update method.
public function decreaseQuantities()
{
foreach (Cart::content() as $item) {
$product = Product::find($item->id);
$newQuantity = $product->quantity - $item->qty;
$product->quantity => $newQuantity;
$product->save();
}
}
Or
public function decreaseQuantities()
{
foreach (Cart::content() as $item) {
$product = Product::find($item->id);
$newQuantity = $product->quantity - $item->qty;
$product->update(['quantity' => $newQuantity]);
}
}
public function checkout(){
if(Request::isMethod('post')){
$product_id = Request::get('product_id');
$product = Product::find($product_id);
Cart::add(array('id' => $product_id, 'name' => $product->name, 'qty' => 1, 'price' => $product->price));
}
$cart = Cart::content();
//increment
if (Request::get('product_id') && (Request::get('increment')) == 1) //this can be if(Request::get('increment')) == 1) < is it possible to submit a POST request without a product id? If so, please check your front-end logic>
{
$item = Cart::search(
function($key, $value) {
return $key->id == Request::get('product_id');
})->first();
Cart::update($item->rowId, $item->qty += 1); //you can also have Cart::update($item->rowId, 'increment'); and do your logic in update method
}
if (Request::get('decrease')) == 1) //check first if $item->qty >0. you might disable your button if quantity is 0
{
$item = Cart::search(function($key, $value) { return $key->id == Request::get('product_id'); })->first();
Cart::update($item->rowId, $item->qty -= 1); //
}
you cannot return a redirect because you are in a loop. I guess you need to put your message in an array like: Product A added successfully, Product B failed, insufficent quantity