Creating a Shopping Cart With Laravel

Creating a Shopping Cart With Laravel

In this tutorial i will demonstrate how to create a shopping cart with php using laravel framework. we will be using laravel 5.5 but you can also use any other version.

 

Requirements:

  • Apache or IIS with PHP >= 5.6.4 .
  • Twitter bootstrap.

 

Installation

In your local development path type this command to install laravel 5.5.

After successful installation be sure that there is a “.env” file in the root path of the application. If not then take a copy of “.env.example” and rename it to “.env” then in type this command in the terminal.

Now laravel successfully installed and you can be sure by visiting http://localhost/shopping-cart/public/.

 

Preparing Database

Every shopping cart must have a products table so we will create it right now.

Go to phpmyadmin and create a new database called “shopping_cart”. Then go to your .env file and set your database name and credentials like this:

 

Next we will be using laravel migration to create tables so ype this command in terminal.

The above command will create a Product model and -m for migration.

Now go to database>migrations and open up the migration we just created for products table.

We will be adding some more fields to the up() function so add this code after $table->increments(‘id’)

Now migrate your tables by this command:

Now if you go to phpmyadmin. This is our database might look like after migration

Now let’s create a Seeder to populate our products table with some data to work with. Run this command to generate a seeder.

Go to database>seeds>ProductsSeeder. Then inside run() function add this code.

Now run this command to seed your data.

Our products table now might look like.

 

Controller & Views

Let’s create a controller for our products and cart called “ProductsController”.

Now these actions in the above controller as shown:

In the above code we created two actions for our products and cart pages and one action for add to cart.

Next we will create three views with dummy data:

  • layout.blade.php: this is the main layout view.
  • products.blade.php: this is the view that will display our products.
  • cart.blade.php: this is the cart view.

In the layout.blade.php copy and paste this code.

As shown in the above code we have included bootstrap and jquery and the main style.css file and in the template header we displayed the cart icon along with sample dummy data.

 

products.blade.php

 

cart.blade.php

 

style.php

 

Routes

At this point go to routes>web.php and add the following routes:

Now visit http://localhost/shopping-cart/public/ and http://localhost/shopping-cart/public/cart you will see our products and cart page respectively.

 

Displaying Products

Go to ProductsController.php in the index function add the below code

Here we fetch all products and sent them to the products view.

Now go to resources>views>products.blade.php and replace the dummy data with the below code

Great work now you can see the actual products by going to http://localhost/shopping-cart/public/

Processing the cart

Now its time to add logic for when user clicks on add to cart button. We will do this in the addToCart function in ProductsController.php and we will store the cart item using laravel session.

In the above code first we fetch the product and checked if it exist or not then we check if there is a cart exist in the session. If cart is empty then we treated the item as the first product and insert it along with quantity and price. If cart not empty we checked if the product already exist and increment its quantity.

 

Displaying the cart contents

At this point we have a cart with products let’s display it in the cart view.

Go to resources>views>cart.blade.php and update the view with below code:

And also update the main layout view layout.blade.php

 

Updating & removing from the cart

Now to remove or update an item in the cart we will create two actions in the ProductsController as follows:

Let’s create two other routes

Now go to resources>views>cart.blade.php and add this code at the bottom

As shown above we added two listeners for buttons update and remove and for each button just send ajax request to update and remove products.

Download the source

 

Conclusion

You learned in this tutorial how to build a shopping cart with laravel. I decided to built this from scratch although there are ready made packages in laravel for shopping carts but to let you understand the basics of shopping cart. You can also extend this tutorial to add functionality of checkout, payments, and more

Share this: