Laravel 7/6 DataTable Ajax CRUD Example Tutorial

In this tutorial you will learn about the Laravel 7/6 DataTable Ajax CRUD Example Tutorial and its application with practical example.

In this Laravel 7/6 Datatables CRUD operation example tutorial I’ll show you how to create a simple crud application using yajra Datatables in laravel 7/6. In this example we will learn how to create a simple crud operation application using Datatables in laravel 7/6. In this laravel 7/6 crud application we will be using jQuery and ajax to delete data from datatable crud app and MySQL database in laravel 7/6 app.

In this tutorial, you will learn to implement ajax based CRUD (Create, Read, Update and Delete) operations using datatable js. In this tutorial, we will be using yajra datatable package for listing of records with pagination, sorting and filter (search) feature.

Laravel 7/6 DataTable Ajax CRUD Example Tutorial

In this step by step guide, we will be creating a simple laravel 7/6 application to demonstrate you how to install yajra datatable package and implement ajax based CRUD operations with datatable js.

  • Step 1: Install Fresh laravel Setup
  • Step 2: Set database Credentials
  • Step 3: Create Migration And Model
  • Step 4: Install Yajra DataTables in App
  • Step 5: Create Route, Controller & Blade View
  • Step 6: Run Development Server
  • Step 7: Live Demo

Step 1: Install Fresh Laravel Setup

First of all we need to create a fresh laravel project, download and install Laravel using the below command

Step 2: Set database Credentials

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Step 3: Create Migration And Model

Now, in this step we will create model and migration file. Please run the following command:

Now go to database/migrations/ and open create_products_table.php file. Then put the following code into this file:

Now, in this step we will create model and migration file. Please run the following command:

The above command will create tables in your database. Now, go to App directory and open Product.php file and then update the following code to into Product.php file as follow

app\Product.php

Step 4: Install Yajra Datatables Package in Laravel

In this step, we will install Yajra Datatables Package via the composer dependency manager. Use the following command to install Yajra Datatables Package.

After Installing Yajra Datatables package, we need to add service provider and alias in config/app.php file as following.

config/app.php

After set providers and aliases then publish vendor run by following command.

Step 5: Create (Controller, Route, Blade)

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

2: Create Controller

Now, lets create a controller named ProductController using command given below –

Now, open app/Http/Controllers/ProductController.php and create some methods to add products, edit product and delete the product. In Product Controller, we need to create some methods:

  • Index()
  • Store()
  • Edit()
  • Destroy()

Index() method

Store() Method

Edit() Method

Delete() Method

Now, update all methods into your ProductController.php file as follow:

3: Create Blade View

Create a Button view

Now, we will create action.blade.php file. This file contains a two-button name edit and delete. So you can update the below code in your action button file.

Next, create a list.blade.php file in resources/views/ folder and update the below code in your product list file.

Script Code

Step 6: Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the Laravel 7/6 DataTable Ajax CRUD Example Tutorial and its application with practical example. I hope you will like this tutorial.