{"id":1297,"date":"2024-08-22T11:03:19","date_gmt":"2024-08-22T11:03:19","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=1297"},"modified":"2024-08-22T11:03:20","modified_gmt":"2024-08-22T11:03:20","slug":"ajax-crud-operations-in-laravel","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/ajax-crud-operations-in-laravel\/","title":{"rendered":"Building a Simple AJAX CRUD Application in Laravel: Add, Edit, Delete, and List"},"content":{"rendered":"\n<p>In modern web applications, <strong>CRUD operations CRUD operations<\/strong>\u2014Create, Read, Update, and Delete\u2014are fundamental features. With Laravel, developing these functionalities is straightforward, In this blog we walk through the manner of building a simple<strong> AJAX-based totally CRUD software in Laravel<\/strong>. We will cover everything from putting in database to the Laravel challenge to writing the code for AJAX-primarily based operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ajax Methods with Laravel Crud<\/h2>\n\n\n\n<p><strong>AJAX (Asynchronous JavaScript and XML)<\/strong> allows you to send and receive data from a server asynchronously, without refreshing the page. This leads to a smoother user experience, as users can interact with the application while data is being processed in the background. In this tutorial, we&#8217;ll demonstrate how to implement <strong>AJAX-based CRUD operations in a Laravel application<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Up the Laravel Project<\/h2>\n\n\n\n<p>First, ensure you have<a href=\"https:\/\/getcomposer.org\/\"> <strong>Composer<\/strong><\/a><strong> installed on your system<\/strong>. Then, create a new <a href=\"https:\/\/laravel.com\/docs\/11.x\/installation\">Laravel project using the following command<\/a>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">composer create-project --prefer-dist laravel\/laravel ajax-crud-app<\/pre><\/div>\n\n\n\n<p>Navigate to the project directory:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">cd ajax-crud-app<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Database Configuration<\/h2>\n\n\n\n<p>Open the <code>.env<\/code> file and configure your database connection settings:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">DB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=ajax_crud_db\nDB_USERNAME=root\nDB_PASSWORD=your_password<\/pre><\/div>\n\n\n\n<p>After setting up the database, create it in your <strong><a href=\"https:\/\/www.mysql.com\/\">MySQL <\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating the Model and Migration<\/h2>\n\n\n\n<p>Next, create a model along with a migration for the entity we will manage. For this example, let&#8217;s create a model called <code>Post<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan make:model Post -m<\/pre><\/div>\n\n\n\n<p>This command creates a <code>Post<\/code> model and a migration file. Open the migration file located at <code>database\/migrations<\/code> and define the schema for the <code>posts<\/code> table:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">public function up()\n{\n    Schema::create('posts', function (Blueprint $table) {\n        $table-&gt;id();\n        $table-&gt;string('title');\n        $table-&gt;text('body');\n        $table-&gt;timestamps();\n    });\n}<\/pre><\/div>\n\n\n\n<p>Run the migration to create the <code>posts<\/code> table in the database:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan migrate<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Up Routes<\/h2>\n\n\n\n<p>Open the <code>routes\/web.php<\/code> file and define the necessary routes for your <strong>CRUD operations<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">use App\\Http\\Controllers\\PostController;\n\nRoute::get('\/', [PostController::class, 'index'])-&gt;name('posts.index');\nRoute::get('\/posts', [PostController::class, 'getPosts'])-&gt;name('posts.get');\nRoute::post('\/posts', [PostController::class, 'store'])-&gt;name('posts.store');\nRoute::get('\/posts\/{id}', [PostController::class, 'edit'])-&gt;name('posts.edit');\nRoute::put('\/posts\/{id}', [PostController::class, 'update'])-&gt;name('posts.update');\nRoute::delete('\/posts\/{id}', [PostController::class, 'destroy'])-&gt;name('posts.destroy');\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Creating the Controller<\/h2>\n\n\n\n<p>Next, create a <code>PostController<\/code> to handle the logic for our <strong>CRUD operations:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan make:controller PostController<\/pre><\/div>\n\n\n\n<p>In the <code>PostController<\/code>, add the following methods to handle the <strong>CRUD operations<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">namespace App\\Http\\Controllers;\n\nuse App\\Models\\Post;\nuse Illuminate\\Http\\Request;\nuse Response;\n\nclass PostController extends Controller\n{\n    public function index()\n    {\n        return view('posts.index');\n    }\n\n    public function getPosts()\n    {\n        $posts = Post::all();\n        return response()-&gt;json($posts);\n    }\n\n    public function store(Request $request)\n    {\n        $post = Post::create([\n            'title' =&gt; $request-&gt;title,\n            'body' =&gt; $request-&gt;body,\n        ]);\n        \n        return response()-&gt;json($post);\n    }\n\n    public function edit($id)\n    {\n        $post = Post::find($id);\n        return response()-&gt;json($post);\n    }\n\n    public function update(Request $request, $id)\n    {\n        $post = Post::find($id);\n        $post-&gt;update([\n            'title' =&gt; $request-&gt;title,\n            'body' =&gt; $request-&gt;body,\n        ]);\n\n        return response()-&gt;json($post);\n    }\n\n    public function destroy($id)\n    {\n        Post::destroy($id);\n        return response()-&gt;json(['success' =&gt; 'Post deleted successfully.']);\n    }\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Building the Blade View<\/h2>\n\n\n\n<p>Create a Blade template to serve as the frontend for the <strong>CRUD operations<\/strong>. In the <code>resources\/views<\/code> directory, create a file named <code>index.blade.php<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;meta name=&quot;csrf-token&quot; content=&quot;{{ csrf_token() }}&quot;&gt;\n    &lt;title&gt;Laravel AJAX CRUD&lt;\/title&gt;\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.5.2\/css\/bootstrap.min.css&quot;&gt;\n    &lt;script src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.5.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;div class=&quot;container mt-5&quot;&gt;\n    &lt;h2 class=&quot;text-center&quot;&gt;Laravel AJAX CRUD&lt;\/h2&gt;\n    &lt;div class=&quot;row&quot;&gt;\n        &lt;div class=&quot;col-md-6&quot;&gt;\n            &lt;form id=&quot;postForm&quot;&gt;\n                &lt;div class=&quot;form-group&quot;&gt;\n                    &lt;label for=&quot;title&quot;&gt;Title:&lt;\/label&gt;\n                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;title&quot; name=&quot;title&quot; required&gt;\n                &lt;\/div&gt;\n                &lt;div class=&quot;form-group&quot;&gt;\n                    &lt;label for=&quot;body&quot;&gt;Body:&lt;\/label&gt;\n                    &lt;textarea class=&quot;form-control&quot; id=&quot;body&quot; name=&quot;body&quot; required&gt;&lt;\/textarea&gt;\n                &lt;\/div&gt;\n                &lt;button type=&quot;submit&quot; id=&quot;saveBtn&quot; class=&quot;btn btn-success&quot;&gt;Save&lt;\/button&gt;\n            &lt;\/form&gt;\n        &lt;\/div&gt;\n        &lt;div class=&quot;col-md-6&quot;&gt;\n            &lt;table class=&quot;table table-bordered&quot;&gt;\n                &lt;thead&gt;\n                    &lt;tr&gt;\n                        &lt;th&gt;ID&lt;\/th&gt;\n                        &lt;th&gt;Title&lt;\/th&gt;\n                        &lt;th&gt;Body&lt;\/th&gt;\n                        &lt;th&gt;Action&lt;\/th&gt;\n                    &lt;\/tr&gt;\n                &lt;\/thead&gt;\n                &lt;tbody id=&quot;postList&quot;&gt;\n                    &lt;!-- Data will be loaded here via AJAX --&gt;\n                &lt;\/tbody&gt;\n            &lt;\/table&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n\n&lt;script&gt;\n    $(document).ready(function() {\n        \/\/ Fetch posts\n        fetchPosts();\n\n        \/\/ Save post\n        $('#postForm').on('submit', function(e) {\n            e.preventDefault();\n            let id = $('#post_id').val();\n            let url = id ? `\/posts\/${id}` : '\/posts';\n            let type = id ? 'PUT' : 'POST';\n            let formData = {\n                title: $('#title').val(),\n                body: $('#body').val(),\n            };\n\n            $.ajax({\n                type: type,\n                url: url,\n                data: formData,\n                dataType: 'json',\n                success: function(response) {\n                    $('#postForm').trigger(&quot;reset&quot;);\n                    $('#saveBtn').text('Save');\n                    fetchPosts();\n                },\n                error: function(error) {\n                    console.log(error);\n                }\n            });\n        });\n\n        \/\/ Edit post\n        $(document).on('click', '.edit-post', function() {\n            let id = $(this).data('id');\n            $.get(`\/posts\/${id}`, function(data) {\n                $('#title').val(data.title);\n                $('#body').val(data.body);\n                $('#post_id').val(data.id);\n                $('#saveBtn').text('Update');\n            });\n        });\n\n        \/\/ Delete post\n        $(document).on('click', '.delete-post', function() {\n            let id = $(this).data('id');\n            if(confirm(&quot;Are you sure want to delete this post?&quot;)) {\n                $.ajax({\n                    type: &quot;DELETE&quot;,\n                    url: `\/posts\/${id}`,\n                    success: function(response) {\n                        fetchPosts();\n                    },\n                    error: function(error) {\n                        console.log(error);\n                    }\n                });\n            }\n        });\n\n        \/\/ Fetch posts\n        function fetchPosts() {\n            $.ajax({\n                type: &quot;GET&quot;,\n                url: &quot;\/posts&quot;,\n                success: function(data) {\n                    let rows = '';\n                    $.each(data, function(key, post) {\n                        rows += `\n                            &lt;tr&gt;\n                                &lt;td&gt;${post.id}&lt;\/td&gt;\n                                &lt;td&gt;${post.title}&lt;\/td&gt;\n                                &lt;td&gt;${post.body}&lt;\/td&gt;\n                                &lt;td&gt;\n                                    &lt;button data-id=&quot;${post.id}&quot; class=&quot;btn btn-primary btn-sm edit-post&quot;&gt;Edit&lt;\/button&gt;\n                                    &lt;button data-id=&quot;${post.id}&quot; class=&quot;btn btn-danger btn-sm delete-post&quot;&gt;Delete&lt;\/button&gt;\n                                &lt;\/td&gt;\n                            &lt;\/tr&gt;\n                        `;\n                    });\n                    $('#postList').html(rows);\n                },\n                error: function(error) {\n                    console.log(error);\n                }\n            });\n        }\n    });\n&lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing AJAX CRUD Operations<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">AJAX for Adding Data<\/h3>\n\n\n\n<p>When the user submits the form, the <code>#postForm<\/code>&#8216;s <code>submit<\/code> event triggers an AJAX request to either store or update the post. This is determined by whether there is an ID present:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$('#postForm').on('submit', function(e) {\n    e.preventDefault();\n    let id = $('#post_id').val();\n    let url = id ? `\/posts\/${id}` : '\/posts';\n    let type = id ? 'PUT' : 'POST';\n    let formData = {\n        title: $('#title').val(),\n        body: $('#body').val(),\n    };\n\n    $.ajax({\n        type: type,\n        url: url,\n        data: formData,\n        dataType: 'json',\n        success: function(response) {\n            $('#postForm').trigger(&quot;reset&quot;);\n            $('#saveBtn').text('Save');\n            fetchPosts();\n        },\n        error: function(error) {\n            console.log(error);\n        }\n    });\n});\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">AJAX for Editing Data<\/h3>\n\n\n\n<p>When a user clicks the &#8220;Edit&#8221; button, an AJAX request is sent to fetch the post data, which is then populated into the form fields:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$(document).on('click', '.edit-post', function() {\n    let id = $(this).data('id');\n    $.get(`\/posts\/${id}`, function(data) {\n        $('#title').val(data.title);\n        $('#body').val(data.body);\n        $('#post_id').val(data.id);\n        $('#saveBtn').text('Update');\n    });\n});\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">AJAX for Deleting Data<\/h3>\n\n\n\n<p>When a user clicks the &#8220;<strong>Delete<\/strong>&#8221; button, an AJAX request is sent to delete the post, followed by refreshing the list of posts:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$(document).on('click', '.delete-post', function() {\n    let id = $(this).data('id');\n    if(confirm(&quot;Are you sure want to delete this post?&quot;)) {\n        $.ajax({\n            type: &quot;DELETE&quot;,\n            url: `\/posts\/${id}`,\n            success: function(response) {\n                fetchPosts();\n            },\n            error: function(error) {\n                console.log(error);\n            }\n        });\n    }\n});\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>AJAX for Listing Data<\/strong><\/h3>\n\n\n\n<p>The <code>fetchPosts<\/code> function sends a GET request to retrieve and display all posts:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">function fetchPosts() {\n    $.ajax({\n        type: &quot;GET&quot;,\n        url: &quot;\/posts&quot;,\n        success: function(data) {\n            let rows = '';\n            $.each(data, function(key, post) {\n                rows += `\n                    &lt;tr&gt;\n                        &lt;td&gt;${post.id}&lt;\/td&gt;\n                        &lt;td&gt;${post.title}&lt;\/td&gt;\n                        &lt;td&gt;${post.body}&lt;\/td&gt;\n                        &lt;td&gt;\n                            &lt;button data-id=&quot;${post.id}&quot; class=&quot;btn btn-primary btn-sm edit-post&quot;&gt;Edit&lt;\/button&gt;\n                            &lt;button data-id=&quot;${post.id}&quot; class=&quot;btn btn-danger btn-sm delete-post&quot;&gt;Delete&lt;\/button&gt;\n                        &lt;\/td&gt;\n                    &lt;\/tr&gt;\n                `;\n            });\n            $('#postList').html(rows);\n        },\n        error: function(error) {\n            console.log(error);\n        }\n    });\n}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Testing the Application<\/strong><\/h2>\n\n\n\n<p>To test your application, start the <strong>Laravel development server<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan serve<\/pre><\/div>\n\n\n\n<p>Visit <code>http:\/\/localhost:8000<\/code> to view your application. You should be able to add, edit, and delete posts without refreshing the page.<\/p>\n\n\n\n<p>In this article, we\u2019ve built a simple <strong>CRUD application in Laravel<\/strong> enhanced with AJAX. By using AJAX, we\u2019ve improved the user experience by making our application more responsive and eliminating the need for page reloads.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In modern web applications, CRUD operations CRUD operations\u2014Create, Read, Update, and Delete\u2014are fundamental features. With<\/p>\n","protected":false},"author":1,"featured_media":1298,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,1],"tags":[210,209,208,207],"class_list":["post-1297","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","category-php","tag-ajax-methods-with-laravel-crud","tag-crud-operations-crud-operations","tag-crud-software-in-laravel","tag-laravel-crud"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=1297"}],"version-history":[{"count":1,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1297\/revisions"}],"predecessor-version":[{"id":1299,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1297\/revisions\/1299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/1298"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=1297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=1297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=1297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}