Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

filkris's avatar

Laravel RESTful Shopping Cart

Hello everyone,

I am working on project where I implemented some kind of "web shop" to provide ability to customers to buy some services/products. At the beginning, I did not know that I will need to build mobile version using Ionic framework. So, I used a session based approach to store services/products in the cart and switched the session driver to Redis - everything works fine.

Now, I am building the API and Ionic app where I came to the part to add services/products to the cart. Since REST should be stateless and it does not have session I need to find a way to save items in cart. Of course, if we take a look in app\Http\Kernel.php, \Illuminate\Session\Middleware\StartSession middleware is missing for api, as expected.

I spent hours and hours to find any good reference or example what is a right way or how to make: "Laravel RESTful Shopping Cart", "Laravel Non Session Cart" etc. and I found almost nothing (just bunch of session or database approaches and a lot outdated Cart packages).

Does anyone have an idea how to organize cart storage, should it be on client side (ex. Ionic storage) or on server side (ex. MySQL database or Redis)?

Does anyone know any good (right way) reference or any premium/free book, tutorial, video course, example, package on how to achieve this?

Many thanks

0 likes
4 replies
artcore's avatar

Just a wild thought...are you using an authentication id for the api unique to the visitor? I would use that to store a json object holding the cart data. You need some sort of state persistence and this is at least without a session. I wouldn't use a cookie as they are unreliable and could be tampered with.

filkris's avatar

In general, I ended up storing data in the database since I needed to support web + app. If you use a session, cart data will be available only on web. Same for an application, if you use a storage on client side, cart data will be available only for application. In that case, you will miss if someone start buying using an application and decide to continue on web.

My solution was to make a class to save products using multiple drivers. So, a session or client side storage driver is used if the customer is not logged in. The database driver is used if the customer is logged in. As an addition, when the customer is authenticated, the data from session or client side will be transferred to the database. On top of that, I used Redis as well, but just to reduce number of MySQL queries.

Standalone Redis solution is not bad too, just be aware of RAM usage. I found a lot of customers actually likes to keep products in their cart and not wish list. So, instead of keeping their cart data on Redis for long time I would make a cron job to move them to database (wish list) after some time.

1 like

Please or to participate in this conversation.