Laravel 10 How To Update Data by Ajax Request Tutorial

Reading Time: 6 minutes

Allowing users to change data easily without page reloads is a hallmark of modern, user-friendly apps in the dynamic landscape of web development.

The driving force behind this interactivity is AJAX requests, and Laravel 10, a sophisticated PHP framework, provides a quick and effective way to update data using AJAX queries. This article will walk you through the process of updating data in Laravel 10 using AJAX requests.

Throughout this article, we’ll go over the steps needed to set up AJAX-based data updates in Laravel 10 and show how they may be used in practise with real-world applications. By the end of this article, you’ll be able to give your web applications the flexibility to update data in a fluid and engaging manner while remaining within the Laravel 10 environment.

Read More: Laravel 10 Re-Usable Component Class Example Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve


Assuming laravel already installed inside your system.

Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.


How To Generate Fake Data in Laravel?

To generate fake data in a very easier way, you can use the concept of Factory.

Once you install laravel setup, you should see these files –

  • User.php a model file inside /app/Models folder.
  • UserFactory.php a factory file inside /database/factories folder.

You can use concept of factory to seed dummy data to this application.

But first you need to migrate your application’s migration to create users table into your database.

Go to tinker shell,

$ php artisan tinker

It will open tinker shell to execute commands. Copy and paste this command and execute to create test data.

>>> User::factory()->count(100)->create()

It will generate 100 fake rows of users into table.

Tinker shell,


Database table,

Read More: Laravel 10 How To Add Pagination Links Example Tutorial


Setup Data Controller

You need to create a controller file,

$ php artisan make:controller UserController

It will create a file UserController.php inside /app/Http/Controllers folder.

Open file and write this complete code into it,


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\View\View;

class UserController extends Controller
    public function index(): View
        $users = User::paginate(10);

        return view('users', compact('users'));

    public function update(Request $request, $id)
            'name' => $request->name,
            'email' => $request->email
        return response()->json(['success' => 'User Updated Successfully!']);

Create Blade Template File

Go to /resources/views folder and create a file with name users.blade.php

Open file and write this complete code into it,

<!DOCTYPE html>

    <title>Laravel 10 How To Update Data by Ajax Request Tutorial</title>
    <link href="" rel="stylesheet">
    <script src=""></script>
    <script src=""></script>
    <meta name="csrf-token" content="{{ csrf_token() }}">


    <div class="container" style="margin-top: 10px;">

        <h3 style="text-align: center;">Laravel 10 How To Update Data by Ajax Request Tutorial</h3>

        <table class="table table-bordered data-table" style="margin-top: 30px;">
                @foreach ($users as $user)
                <tr data-id="{{ $user->id }}">
                    <td>{{ $user->id }}</td>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>
                        <a href="javascript:void(0)" class="btn btn-primary edit-user">Edit</a>
        {!! $users->withQueryString()->links('pagination::bootstrap-5') !!}


    <!-- Show user data -->
    <div class="modal fade" id="userEditModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Edit User</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>


                <div class="modal-body">
                    <input type="hidden" id="user-id" name="id"></span>
                    <p><strong>Name:</strong> <br /> <input type="text" name="name" id="user-name"
                    <p><strong>Email:</strong> <br /> <input type="email" name="email" id="user-email"

                <div class="modal-footer">
                    <button type="button" class="btn btn-success" id="user-update">Update</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>

    <script type="text/javascript">
    $(document).ready(function() {

            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

        When click user on Edit Button
        $(document).on('click', '.edit-user', function() {

            var userURL = $(this).data('url');



        $(document).on('click', '#user-update', function() {

            var id = $('#user-id').val();
            var name = $('#user-name').val();
            var email = $('#user-email').val();

                url: '/users/' + id,
                type: 'PUT',
                dataType: 'json',
                data: {
                    name: name,
                    email: email
                success: function(data) {
                    $("tr[data-id=" + id + "]").find("td:nth-child(2)").text(name);
                    $("tr[data-id=" + id + "]").find("td:nth-child(3)").text(email);




Add Route

Open web.php from /routes folder and add these route into it.

use App\Http\Controllers\UserController;

Route::get('users', [UserController::class, 'index']);
Route::put('users/{id}', [UserController::class, 'update'])->name('users.update');

Application Testing

Run this command into project terminal to start development server,

php artisan serve


List of all users with Edit button.

Read More: Laravel 10 How To Get Data by Ajax Request Tutorial

Once you click on Edit button, it will show the existing information of user with Update button. Update users data and submit.

That’s it.

We hope this article helped you to Laravel 10 How To Update Data by Ajax Request Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more