Create Ecommerce Website using Laravel.

Ecommerce websites 

Introduction :

Ecommerce store owners understand the importance of marketing and developing a strong web presence. They create beautiful storefronts, invest in advertising campaigns, and build email marketing sequences to attract and win over customers. so we are made ecommerce websites in this blog i write fully about the making of a ecommerce website using laravel and what are important things will be in a ecommerce websites .



Requirements for make a Laravel project in our system :

  • Installation requirements Install the composer latest version and install Xampp in the working system.
  • And required a text editor like Microsoft Vs code ,Sublime text editor or notepad etc. for edit our project code .
  • Then select the folder directory  in xampp and go into the htdocs folder then open in the command prompt .
  • Then write "composer global require laravel/installer"  then hit enter  and  "laravel new example-app"  and "cd example-app" and "php artisan serve" to run the project.
  • Then open the project folder via vscode or any text editor and ready edit the project and make our ecommerce project.

Basic Ecommerce Features :

  • A home page list all products.
  • A product description page.
  • A checkout page to provide address details and payment information.
  • A cart page for how many products customer will be selected.
  • A dashboard for vendors to add/delete/update the product information and check sales, order and shipment details.
  • A customer dashboard to check the product purchased and shipment status.

Benefits of Ecommerce site make using Laravel :

  • Laravel is cost-friendly.
  • Laravel is scalable.
  • Laravel is easy to test.
  • Laravel offers a manageable shopping cart.
  • Laravel offers highly optimized e-commerce experience .

User Roles Needed :

  • Admin: The Administrator of the site.
  • Buyer: The customers who purchase goods from the site.
  • Vendors :The  sellers who sell the products on the site.

Database connection:

  • First we create a database in the mqsql like a name ecommerce it will be connected to our Laravel project .
  • In the project find the .env file on that file we change some line DB_connection="our database type like mysql"  "DB_connection=mysql" like database port ,name ,username and password these are also important to change.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ecomm
DB_USERNAME=root
DB_PASSWORD=

Create Models:

  • 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 our database tables.
  • Create model in the terminal  by the command "$ php artisan make:model modelname" .
  • By this command we make some models like product and user for our products and the users "$ php artisan make:model Product"&"$ php artisan make:model User" .

Create Migrations:

  • Migrations are like version control of your database schema in Laravel. It allows you to easily create and manage the database. 
  • Create model in the terminal  by the command "$ php artisan make:migration create_users_table" and "$ php artisan make:migration create_products_table".
Create Seeders:
  • Laravel created seed classes to push test data into our database.
  • Create seeder by "$ php artisan make:seed table-name" for our project "$php artisan  make:seed Userstable" and "$php artisan  make:seed Procductstable".
  • By the seeding in products table we giving the value in all columns and give all datas to the user table also .
Create view pages:
  • Create a registration page with a form like user name , email and password with some design using bootstrap or others .
  • Then like registration page we make a login page with a form contains email and password and a login button .
  • And make a home page with some designs with our products display view it comes after the login .

Create Routes :

  • Create some Routes for our pages like login registration and home page and some extra pages in futures in routes we define url.
  • Making routes by a syntax is Route::('method')('/url',['controller-name'::class,'function-name']);
Route::post("/register", [UserController::class,'register']);
Route::post("/login", [UserController::class,'login']);
  • For our project make login route by  Route::post('/login',[UserController::class,'login']); like this for registration and the home pages also.
Create Controllers :

  • Create controllers by the command in terminal is "$ php artisan make:controller controller-name".
  •  Make some controllers for our by using this command liken $ php artisan make:controller UserController and $ php artisan make:controller ProductController .
  • This controllers are working based on the MVC pattern.

Implementing Controllers : 

  • After making the controllers and routes we should connect the routes to the controllers.
  • The connection will be made with a function that function is made in the controller.
class UserController extends Controller
{
    //
    function login(Request $req)
    {"functionality code"}}

like this we made the function .
  • By this way make funtions for registartion , login and home page .
  • In product controller for Home page
class ProductController extends Controller
{
    //
    function index()
    {
 "functionality code"
}}
  • In this controller we also use the model on the top  like below.
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
  • Like this way we implement the controller for all the routes also for login and registration and some more future routes.

Login Functionality 

  • We are doing login via some methods like use session , jetstream ,breeze etc 
  • In our project use session by get the data from the user it save on the session and check using controller with database then proceed to login . 
  function login(Request $req)
    {
        $user = User::where(['email'=>$req->email])->first();
       
        if (!$user || !Hash::check($req->password, $user->password))
        {
            return "Username or password is not matched";
        }
        else{
            $req->session()->put('user',$user);
            return redirect('/');
        }
    }
like this function.

Registration 

  • For registration made a route with post method and function for create a new user in the database.
  • In function write query for post user data to our database in users tables and for password encryption we use hash so is use in top of controller.
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
Function is .
  function register(Request $req)
     {
       
          $user = new User;
          $user->name=$req->name;
          $user->email=$req->email;
          $user->password=Hash::make($req->password);
          $user->save();
          return redirect('/login');
        }

Home page

  • For our home page first seeding some products details to the database and made a view  then make a route and a controller to view it in the home page .
  • And home page also contains login button and register button before login then cart list and the order list its working after the login by user .
  • Our route like
Route::get("/", [ProductController::class,'index']);

  • And function in the controller will be get all data from the database.
  function index()
    {
        $products=Product::all();
        return view('product',compact('products'));
    }

  • $products it will be used in the blade view page 
<div class="carousel-inner">
      @foreach($products as $item)
      <a href="detail/{{$item['id']}}">
    <div class="carousel-item {{$item['id']==1?'active':''}} ">
      <img src="{{$item['gallery']}}" class=" custom-slider mx-5 px-5" >
      <div class="carousel-caption d-none d-md-block text-dark">
        <h5>{{$item['name']}}</h5>
        <p>{{$item['description']}}</p>
      </div>
   </a>
    </div>
    @endforeach
</div>
like this .

Routes :

Routes we used in the project -
Route::post("/login", [UserController::class,'login']);
Route::get("/", [ProductController::class,'index']);
Route::get("/detail/{id}", [ProductController::class,'detail']);
Route::get("/search", [ProductController::class,'search']);
Route::post("/add_to_cart", [ProductController::class,'addToCart']);
Route::get("/cartlist", [ProductController::class,'cartList']);
Route::get("/removecart/{id}", [ProductController::class,'removeCart']);
Route::get("/ordernow", [ProductController::class,'orderNow']);
Route::post("/orderplace", [ProductController::class,'orderPlace']);
Route::get("/myorders", [ProductController::class,'myOrders']);
Route::view("/register", 'register');
Route::post("/register", [UserController::class,'register']);
Route::post("/login", [UserController::class,'login']);

Then we make function in the controller for these routes and the view pages by the above method ..


After defining the routes and the controllers in the project then we will be designing the front with the blade.php file with bootstrap and also use some JavaScript frame works to build the core front end with that we can create separate dashboard for admin and the users then vendors also .And make separate authentication for them.

For the checkout process we can use any one of our favorite payment processors which can provide integration into the JavaScript and php frame work  .After the completion of front end and back end process , run the below command in terminal to run your application.

$ php artisan serve

Conclusion:

   These are the major process and important functionality of create a Ecommerce website using Laravel. By these concepts we made some more pages with routes and controllers it will give us a perfect user experience and convenient ecommerce site . By using of Laravel to make a site its easy to understand and secure for users and us . And  also Laravel is user friendly and cost friendly .

We hope this blog might have you provided you with the basic understanding of building an ecommerce application in Laravel and build a sample site with this basic ideas if you have any doubt occurs leave it in the comments below . 





Comments