Laravel 5 Ajax Tutorial

Laravel 5 Ajax Tutorial

Let us start to learn how to use Laravel 5 Ajax. It is not complicated for the users who are all familier with ajax in php and also we are goig to
learn how to use curd functionality in laravel. Lets see how to use laravel 5 ajax in application

I have Categories table, Lets see how we are going to make curd for this,

1. Create Categories table.
2. Seed Static values to table // Just for our usage.
3. Create Model.
4. Create Route.
5. Create Controller.
6. Create View File with Required Coding.

These are the basic steps we need to follow to implement laravel 5 ajax.

1. Create Categories table.

I am using Migration Command to create a table .

It will create create_category_table.php file under migrations folder.


Here I have described the fields which all required for categories table.

Now , Migarte categories table

It will create categories table in to your database.

2. Seed Static values to table

Below command will create seeder file under seeds folder


Seed this values to categories table by using below command.

3. Create Model

My Model Category.php will look likes Below

4. Create Route

5. Create Controller

My CategoryController Will looks like below

index() function :

Here I will fetch all the categories and will pass the categories to index page.

createorupdate() function :
Here based on action I will Create or Update or Delete the Category.

sluggable() Function :

Which will convert the given string as sluggable.

6. Create View File with Required Coding.

Under Views/Category/index.balde.php file which contains the cods of category page.

List All Categories

Add New => Which will makes pop up to create a new category.

The pop up will be :

Delete and Save buttons are dealing with doAction() javascript functions :

The doAction() function will be :

id => Category Id, If the id is 0 it will insert the record else will update the record.
action => action should be delete or update.

updateAction() function will fetch category name and udate in pop up text box and will change the save button doAction() function parameters.

clearInputs() function will clean the pop up text box and Save button parameters.

My Full index.balde.php code :

Here Am extending my app.balde.php filde under layouts folder. Which is global for entire application. You can call your global file.

List Category Sample :


Add Category Sample :


Edit Category Sample :


Thats all, Now you can start using ajax in your application.
If you like this article don’t forget to share and comment.
Happy Coding!!!.