How to update the stored cart. I want to update cart which has saved before when i have added single product into to the cart it save the cart but when i want to add more product into the same saved cart. It generate a error the identifier already exist.
I had similar problem.
So deleted that record (finding by identifier and instance) before storing the cart in table.
Then only I called Cart::instance($instance_name)->store($identifier);
May be there is some simple solution but what I did was that I create a new ShoppingCart model:
ShoppingCart.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ShoppingCart extends Model
{
protected $table = 'shoppingcart';
public static function deleteCartRecord($identifier, $instance)
{
$cart = static::where('identifier' , $identifier)->where('instance', $instance);
$cart->delete();
}
}
then in my controller I did this.
<?php
use App\ShoppingCart;
use Auth;
...
public function addToWishList()
{
....
$this->storeCart();
....
}
private function storeCart()
{
if(Auth::check())
{
$user_id = Auth::id();
ShoppingCart::deleteCartRecord($user_id, 'wishlist');
Cart::instance('wishlist')->store($user_id);
}
}
...
I indeed believe there is currently no way to simply delete the stored cart. Let me think about this. For now the workaround above should work.
why my cart content is not saved in the database. I have following code.
public function addToCart(Request $request)
{
$product = Product::where('id', '=', $request->product_id)->first();
$products_id = Cart::content()->pluck('id')->toArray();
ShoppingCart::deleteCartRecord(1, 'shopping');
Cart::add($product->id, $product->name, 1, $product->price, ['class' => $request->class, 'location' => $request->location]);
Cart::instance('shopping')->store(1);
return redirect()->route('cart.view');
}
You鈥檙e calling the add method on the default instance, and store on another (shopping) instance.
Cart data is save as follow.
Cart::instance('shopping')->add($product->id, $product->name, 1, $product->price, ['class' => $request->class, 'location' => $request->location]);
Cart::instance('shopping')->store(1);
O:29:"Illuminate\Support\Collection":1:{s:8:"
but when i am trying to fetch the store cart data it return null. what is the problem ?
+1
how to fetch the stored cart data
after looking at some source code
i found that Cart::restore method will delete record in DB
AND put that record into current session(cookie)
that means if you call Cart::restore, the stored data is already placed in the session stroage
so maybe i'll call restore and display and store every time
like this:
public function __construct()
{
$this->middleware('auth');
$this->middleware(function ($request, $next) {
Cart::restore(Auth::id());
return $next($request);
});
}
public function __destruct()
{
Cart::store(Auth::id());
}
I'm having some issue myself.
I'm retrieving a cart from the DB and i'm trying to remove it's content, but ->pull() doesn't work for me?
And when I'm done, I am supposed to serialize() again and just do the update right?
`
public function restoreFromDB($identifier)
{
if( ! $this->storedCartWithIdentifierExists($identifier)) {
return;
}
$stored = $this->getConnection()->table($this->getTableName())
->where('identifier', $identifier)->first();
$storedContent = unserialize($stored->content);
return $storedContent;
}
`
`
$cartContent = Cart::restoreFromDB($reservation->buyer_id);
if($cartContent) {
foreach($cartContent as $cartItem) {
if($cartItem->id == $ticket->id) {
$cartContent->pull($cartItem->rowId);
}
}
$cartContent = serialize($cartContent);
DB::table('shoppingcart')
->where('identifier', $reservation->buyer_id)
->update(['content' => $cartContent]);
}
`
The answer is simple , I tried and you can restore the cart from database to session and the items will be auto inserted into the database with the new added products.
@hldh214 could u kindly give me an idea on where u r writing this construct and destruct code
@Anum1212 in your Controller
@hldh214 yes i am aware of that but i meant which controller.(should have written the question properly) but i have finally solved the problems THANKS to ur piece of code. i was initially calling it in a wrong controller. thank u ver much for sharing ur knowledge with us.
note
In case there is a newbie like me out there reading this post
i wrote hldh214's code in my controller that deals with all the cart functions like add update view etc.
so maybe u could try writing it there too.
@Anum1212 the destruct and construct function are in the default shoppingcart controller?
@nadamahmoud2711
i have my own cart controller and in that i wrote the following code
public function __construct()
{
$this->middleware('auth');
$this->middleware(function ($request, $next) {
Cart::instance('shopping');
Cart::restore(Auth::id());
return $next($request);
})
public function __destruct()
{
Cart::instance('shopping')->store(Auth::id());
}
similar code can be used for wishList etc
That's my way:
public function store($id=false) {
$user = Sentinel::check();
if ($user) {
$identifier = $user->id;
$content = $this->getContent();
if ( DB::table('shoppingcart')->where('identifier', $identifier)->exists() ) {
DB::table('shoppingcart')->update([
'instance' => $this->currentInstance(),
'content' => serialize($content)
], [
'identifier' => $identifier,
]);
} else {
DB::table('shoppingcart')->insert([
'identifier' => $identifier,
'instance' => $this->currentInstance(),
'content' => serialize($content)
]);
}
}
}
after looking at some source code
i found thatCart::restoremethod will delete record in DB
AND put that record into current session(cookie)
that means if you callCart::restore, the stored data is already placed in the session stroage
so maybe i'll callrestoreand display andstoreevery time
like this:public function __construct() { $this->middleware('auth'); $this->middleware(function ($request, $next) { Cart::restore(Auth::id()); return $next($request); }); } public function __destruct() { Cart::store(Auth::id()); }
hi thank you for your share it worked for me.
but there is a problem to my cart badge count.
if i logout, session will end and when i login in other pages beside cart, i did not get cart count.
i have to view the cart page to retrieve cart data.
how to make it available for the cart badge count to be available in all pages.?