laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

Database storage question

Open n-osennij opened this issue 6 years ago • 3 comments

If I have storage and wishlist I register wishlist in WishListProvider

use Darryldecode\Cart\Cart;
use Illuminate\Support\ServiceProvider;

class WishListProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('wishlist', function($app)
        {
            $storage = new DBStorage(); <-- Your new custom storage
            $events = $app['events'];
            $instanceName = 'cart_2';
            $session_key = '88uuiioo99888';
            return new Cart(
                $storage,
                $events,
                $instanceName,
                $session_key,
                config('shopping_cart')
            );
        });
    }
}

But how can I save it for each individual user? What is $session_key = '88uuiioo99888';? If I use it, in my db all withlists saved like one row with ID 88 (88uuiioo99888 trimmed to 88). In servce provider I can't use Auth::user()->id to set user ID as $session_key.

My problem is next - all wishlists of many users are saved like one global wishlist with ID (key) 88.

So how can I save wishlists for each user separatly?

n-osennij avatar Jun 20 '19 19:06 n-osennij

Also wondering about this. @n-osennij did you find a solution?

RiaanZA avatar Oct 29 '19 08:10 RiaanZA

Got the same question? any clues?

Gatts10 avatar Jun 21 '20 21:06 Gatts10

You can use it as the default cart example.

For example, have a look below.

public function addWishList(Request $request)
    {
        $uuid = $request->input("uuid");

        $wish_list = app('appcart'); //can be app('wishlist'), I chose to use appcart as a singleton in the provider
        $wish_list->session($uuid); //change to the individual user for storage

        $wish_list->add([
            'id' => $request->input("id"),
            'name' => $request->input("name"),
            'price' => $request->input("price"),
            'quantity' => $request->input("quantity"),
            'attributes' => [
                'size' => $request->input("size")
            ]
        ]);

        $items = [];
        $wish_list->getContent()->each(function ($item) use (&$items) {
            $items[] = $item;
        });

        return Helper::jsonResponse([
            'success' => true,
            'data' => $items,
        ]);
    }

    public function getWishList(Request $request)
    {
        $uuid = $request->input("uuid");
        $wish_list = app('appcart'); //can be app('wishlist'), I chose to use appcart as a singleton in the provider
        $wish_list->session($uuid); //change to the individual user for retrieval

        $items = [];
        $wish_list->getContent()->each(function ($item) use (&$items) {
            $items[] = $item;
        });

        return Helper::jsonResponse([
            'success' => true,
            'data' => $items,
        ]);
    }

I have another question though, in my db the cart_data field is always equal to O:32:"Darryldecode\Cart\CartCollection":1:{s:8:", for all users, but when retrieving, it gets different and correct cart data for each user. Wondering how to make sense of this cart_data field, seems like the actual data is stored elsewhere.

Any clue where I need to look at to see the actual data that is being stored.

Thanks

sanjeetchand avatar Nov 06 '20 20:11 sanjeetchand