Laravel 6 Ajax CRUD Application

In this tutorial you will learn about the Laravel 6 Ajax CRUD Application and its application with practical example.

Laravel 6 Ajax CRUD Application

In this Laravel 6 Ajax CRUD Application tutorial, you will learn to create a simple ajax based CRUD (Create, Read, Update and Delete) application in laravel 6. In this step by step tutorial, we will be creating a simple blog application with post table and implement ajax based CRUD operations in laravel 6.

Install Laravel 6 Using Command

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

Configure Database In .env file

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.

.env

Generate Application Key

Open terminal and switch to the project directory and run the following command to generate application key and configure cache.

Set Default String Length

Locate the file “app/Providers/AppServiceProvider”, and add following line of code to the top of the file

and inside the boot method set a default string length as given below –

So this is how “app/Providers/AppServiceProvider” file looks like –

Create Database Table and Migration

Now, we have to define table schema for posts table. Open terminal and let’s run the following command to generate a migration file to create posts table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –

Run Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the posts table will be created in database along with migrations, password_resets and users table.

Create Model

Next, we need to create a model called Post using below command.

Once, the above command is executed it will create a model file Post.php in app directory. Next, we have to assign fillable fields using fillable property inside Post.php file. Open app/Post.php file and put the following code in it –

app/Post.php

Create Controller Using Command

Next, we have to create a controller to handle Ajax CRUD Operations. Create a controller named AjaxPostController using command given below –

Once the above command executed, it will create a controller file AjaxPostController.php in app/Http/Controllers/ajaxcrud directory. Open the ajaxcrud/AjaxPostController.php file and put the following code in it.

app/Http/Controllers/dtable/AjaxPostController.php

Create Blade / View Files

In this step, we will create view/blade file to perform CRUD Operations. Lets create a blade file “index.blade.php” in “resources/views/ajaxcrud/” directory and put the following code in it respectively.

resources/views/ajaxcrud/index.blade.php

Create Resource Routes

After this, we need to add a resource route in “routes/web.php“. Lets open “routes/web.php” file and add following route.

routes/web.php

Start 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 –

http://localhost:8000/ajax-posts

Output:-

laravel-6-ajax-crud-application-tutorial-with-example-1

Click “Add New Post” to submit new post.

laravel-6-ajax-crud-application-tutorial-with-example-2

Click “Edit” button to edit corresponding post.

laravel-6-ajax-crud-application-tutorial-with-example-3

Click “Delete” button to delete corresponding post.

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