Krayin CRM
Read Story

How to start eCommerce website in Laravel? – Part 1

Since the dawn of last decade, if anything that has grown rapidly and faster is the internet which has paved the way to one of it’s biggest usage and today’s world and that’s an eCommerce.

Technology has been at the helm of this buzzword and the urge to improve services and provide greater satisfaction, developers around the world has been eyeing on PHP, JAVA, Python, MEAN, Ruby On Rails etc to ease the work and providing a better solution.

Most noteworthy, with the launch of open source products and increased dependency on the MVC pattern of architecture, Laravel, an open source web framework was launched in 2011 by Taylor Otwell.

With the aim of simplifying web development experience by easing the common tasks such as routing, authentication, sessions, and caching Laravel rosed from there and since has been the popular choice by most of the developers around the world.

In this series, we are going to discuss how you can create an eCommerce application using Laravel as a web framework. With that, you can use any javascript framework for building user interfaces like Vue.js, React, Node.js, Angular JS  or Backbone.js.

We will be dividing the series into two parts. Do let us know in comments regarding your views.

Prior Requirements

Basic eCommerce Feature

Let us list down some basic features that are required in an eCommerce application.

Define User Roles

Next, we need to define the user roles or parties that will be there in an eCommerce system. Two parties are involved at the granular level:

Store Details

We need to define components of the various parts of the store. Apart from User, there are products that are stored. Once they are ordered, you need to keep track of their inventory and delivery details.

We can break down the components as below:

Userusername, password, role
Productname, description, price, quantity, image
Orderproduct, user, stock, delivery_status, address

Creating controllers for operations

Laravel gives privilege you make controllers and use them while defining a route to any URL.

Based on the operation, you can define a controller for each.

User profileUserController
View product listingProductController
View a single productProductController
Edit a productProductController
Add a new productProductController
Order productOrderController
View all ordersOrderController
View a single order informationOrderController
Deliver an order OrderController

Creating a Laravel Application

To create a new application, run the below command in the Laravel CLI

Move to the new directory created. This will be the root directory from where you need to run the other Laravel related commands.

Setting Database for the application

Here we are going to use MySQL as a relational database management system.

Create a database.

Next, open your .env file and enter the following

Run the migrations to create the database tables for our application and seed it:

Creating a Model for Application

Model in MVC architecture holds business logic allows you to interact with the database. It provides the use of functions to perform a basic operation with your database tables.

Laravel installation provides User Model already created. Here we will create models for Product and Order.

Next, open the app/User.php file and replace the contents with the following:

Laravel introduce SoftDeletes in models that way we can’t remove from the database but if remove a record from the front side then it doesn’t show record on front. So we can retrieve the record from the database if we remove the wrong row.

$fillable is an array that contains all those fields of the table and can be filled using mass-assignment

Open app/Product.php file and edit as follows:

This is similar to the User model and also has an order method to establish a relationship with Order.

Now, open app/Order.php file and edit as follows:

This model represents similar to the other except the relationship belongsTo which show order is related to which user and for what product.

Defining Migrations

Migrations are like version control of your database schema in Laravel. It allows you to easily create and manage the database. Like a git, you can easily share your database schema with your team and allow them to manually add the column to the schema.

The Laravel Schema facade provides support for creating and manipulating tables in the database systems.

To create a migration, use the below command

Open the create_users_table migrations file in the database/migrations directory and replace the content with the following:

Here we have defined the columns in the table along with the attributes.

Next, open the create_products_table migrations file in the database/migrations directory and replace the code with the following:

Finally, open the create_orders_table migrations file in the database/migrations directory and replace the contents with the following:

Creating Seeders for Application

Laravel created seed classes to push test data into your database. This allows testing to be quicker and easier. Below we are going to make seed classes for User and Product and provide test data.

Create a seeder class by running the command below:

Open the file UserTableSeeder.php at the database/seeds directory and replace the content with the following:

The seeder class above will create a new admin user in the database.

Make another seeder class for our products table:

Open the database/seeds/ProductTableSeeder.php file and replace the contents with the following:

After that, you need to edit the database/seeds/DatabaseSeeder.php, which actually invokes the seeders:

When we execute the command to seed our database the DatabaseSeeder class is called and it calls the run which in turn class the seeder classes we had set up to run.

That’s all in the first part if this series where we have created the database, defined models and added migrations.

Let us know in comments on your thoughts regarding the article and stay tuned for the second part of the article.

. . .

Comments (4)

Add Your Comment

  • How does Bagisto Compares with OpenCart ? - Bagisto
    Bagisto will be a great competitor in the future. It will also compete with the all the e-commerce ecosystem technology running now like Magento2, Shopify, and WooCommerce.
  • Barakatalan
    Thanks for sharing such Information.E-Commerce is one of the emerging platforms.
  • Andrew
    I think the system is a little too simplified.. products should have been assigned to categories
    • saurav pathak
      Hi Andrew,

      If you are referring to Bagisto, then here products need to be assigned to categories manually. How you are referring here product should have been assigned to categories?

  • css.php