DEV Community

Pierre-Guillaume Laurin
Pierre-Guillaume Laurin

Posted on • Originally published at

Using Laravel for E-Commerce: Tutorial & Live Demo

PHP is, in some ways, the backbone of the Internet. I mean, it is at the core of WordPress, which powers 39.9% of all the websites today. It is also used by the biggest social media; Facebook.

But I have to admit, I’ve never had the chance to explore it fully.

Today is the day I jump into PHP. However, I’ll begin my journey with the darling framework that is Laravel (I’ve heard it makes it easy to build secure, scalable full-stack projects from the start!).

Jumping in PHP

More especially, I’ll try to build a Laravel-powered e-commerce web app.

Hopefully, this will show you the benefits Laravel can bring to your next e-commerce venture.

Here’s what the tutorial will cover:

  • Project Setup with Sail
  • Basic product listing generation, migration, and seeding
  • Using advanced models for product options
  • Front-end wiring
  • Integrating Snipcart

Time to put my PHP skills to test!

Laravel & e-commerce: quick context

Laravel is an open-source PHP web framework used to build sites & web apps.

It has a smooth learning curve, removes some quirks of building with PHP, and has many modern built-in libraries. Some say it's the Ruby on Rails PHP equivalent.

With Laravel, you can leverage Composer to manage dependencies & packages. Many useful packages allow you to fast-track your Laravel development. Think stuff like debugging, authentication, API interactions, etc. Sites like Packagist & Packalyst are great resources to find helpful Laravel packages.

Since September 2020, Laravel 8 is publicly available. It introduces features such as Laravel Jetstream, model factory classes, migration squashing, job batching, improved rate-limiting, dynamic Blade components, Tailwind pagination views, time testing helpers, and more.

Tools for e-commerce on Laravel?

Of course, there are existing e-commerce packages for Laravel. They can help to set up e-commerce functionalities on your Laravel app quickly.

Here’s a curated list of the most popular ones:

  • Bagisto — Code-driven & multi-featured Laravel package for e-commerce. Free & open-source.
  • Aimeos — A Laravel e-commerce package and online shop solution. Free & open-source.
  • AvoRed — A modular and customizable PHP shopping cart. Free & open-source.
  • Vanilo — This one’s actually an e-commerce framework (like Magento or Prestashop) for Laravel.
  • GetCandy — An headless Laravel e-commerce API. Free & open-source.

So, where does that lead us?

The three e-commerce options with Laravel are pretty much:

  1. Pick a Laravel-powered CMS and add custom e-commerce to it.
  2. Use e-commerce packages built to extend your app.
  3. Build your own e-commerce application from the ground up.

First one? Fast & easy. That’s what we did for the first version of this post, using PyroCMS and then integrating Snipcart as the custom shopping cart.

Second one? Interesting. We might do it in another post at some point!

Third one? Close to what we're doing here. But building the whole application from the ground up, including e-commerce capabilities, would be highly time-consuming.

Instead, I’ll show you how you can build a store from scratch using pure Laravel and then adding Snipcart to enable e-commerce and shopping cart customization.

All these options have in common the benefits you’ll win overusing a full-on e-commerce CMS—high scalability, easy customization, detached e-commerce functionalities, etc.

I'm thrilled to be operating inside a Laravel project for this demo. Unlike many of our Jamstack/static site tuts, it'll be super easy to handle any backend logic for our store.

Tutorial: building an e-commerce website with Laravel

Laravel E-Commerce Website With Snipcart


I assume you already have a working installation of PHP and Composer running. If not, you can install them here and here.

For this tutorial, we will use Sail.

Introduced with Laravel 8, it is a light-weight command-line interface for interacting with Laravel’s default Docker development environment that will streamline our development environment and database setup. Sail will allow us to quickly setup a development environment and database completely isolated from our local environment.

Since Sail interacts with a Docker environment, that means you will also need Docker Desktop (a great tool for app development these days). If you prefer, of course, you could plug in a local SQL database or use Homestead from Laravel. The latter is an all-in-one vagrant image that allows you to get started quickly.

→ Read the full post here

Top comments (0)