php - How to update cart in one function using GloudemansShoppingcart in Laravel
Get the solution ↓↓↓I have a problem with theGloudemans\Shoppingcart
module, maybe somebody's using it in his shop or he's using it.
For two days now, I've been trying to write a code that will update my card in oneadd
function but it doesn't work.
Why isn't this working, and how can I fix it?
public function add(Product $product, Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($product) {
return $cartItem->id === $product->id;
});
if ($duplicates->isNotEmpty()) {
MyFlash::success('Item is already in your cart!');
return redirect()->route('shop.cart.index');
}
Cart::add($product->id, $product->name, 1, $product->price)
->associate('App\Product');
return redirect()->route('shop.cart.index')->with('success_message', 'Item was added to your cart!');
}
Answer
Solution:
I believe what you are missing is the Buyable interface, As they have also mentioned here
"If your model implements the Buyable interface and you used your model to add the item to the cart, it will associate automatically."
You can find the buyable interface in Gloudemans\Shoppingcart\Contracts\Buyable;
<?php
namespace App\Product;
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Illuminate\Database\Eloquent\Model;
class Product extends Model implements Buyable
{
// ...your code...
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 7.3.0".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.