<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CRUD operations Archives - Artificial Intelligence</title>
	<atom:link href="https://www.aiuniverse.xyz/tag/crud-operations/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.aiuniverse.xyz/tag/crud-operations/</link>
	<description>Exploring the universe of Intelligence</description>
	<lastBuildDate>Fri, 23 Aug 2024 09:24:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Laravel 11 AJAX CRUD Tutorial: Simple Step-by-Step Guide</title>
		<link>https://www.aiuniverse.xyz/laravel-11-ajax-crud-tutorial-simple-step-by-step-guide/</link>
					<comments>https://www.aiuniverse.xyz/laravel-11-ajax-crud-tutorial-simple-step-by-step-guide/#respond</comments>
		
		<dc:creator><![CDATA[Maruti Kr.]]></dc:creator>
		<pubDate>Fri, 23 Aug 2024 09:24:13 +0000</pubDate>
				<category><![CDATA[laravel]]></category>
		<category><![CDATA[AJAX CRUD]]></category>
		<category><![CDATA[AJAX integration]]></category>
		<category><![CDATA[Bootstrap modals]]></category>
		<category><![CDATA[CRUD application]]></category>
		<category><![CDATA[CRUD operations]]></category>
		<category><![CDATA[Laravel 11]]></category>
		<category><![CDATA[Laravel AJAX example]]></category>
		<category><![CDATA[Laravel tutorial]]></category>
		<category><![CDATA[Step-by-step guide]]></category>
		<category><![CDATA[web development]]></category>
		<guid isPermaLink="false">https://www.aiuniverse.xyz/?p=19072</guid>

					<description><![CDATA[<p>Introduction: In this blog, you&#8217;ll learn how to develop a Laravel 11 AJAX CRUD application through an easy step-by-step tutorial. But before we dive in, let&#8217;s start <a class="read-more-link" href="https://www.aiuniverse.xyz/laravel-11-ajax-crud-tutorial-simple-step-by-step-guide/">Read More</a></p>
<p>The post <a href="https://www.aiuniverse.xyz/laravel-11-ajax-crud-tutorial-simple-step-by-step-guide/">Laravel 11 AJAX CRUD Tutorial: Simple Step-by-Step Guide</a> appeared first on <a href="https://www.aiuniverse.xyz">Artificial Intelligence</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="413" src="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-13-1024x413.png" alt="" class="wp-image-19073" srcset="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-13-1024x413.png 1024w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-13-300x121.png 300w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-13-768x309.png 768w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-13.png 1328w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading"><strong>Introduction:</strong></h2>



<p>In this blog, you&#8217;ll learn how to develop a Laravel 11 AJAX CRUD application through an easy step-by-step tutorial. But before we dive in, let&#8217;s start with some introductions:</p>



<p>Laravel is a free and open-source PHP web framework designed for building web applications using the MVC (Model-View-Controller) architectural pattern. It simplifies web development by providing built-in features that speed up and ease the process.</p>



<p>AJAX (Asynchronous JavaScript and XML) is a collection of web development techniques that use various web technologies to enable web applications to operate asynchronously, enhancing user experience.</p>



<p>CRUD stands for Create, Read, Update, and Delete—the four fundamental operations in any programming environment, typically related to database management:</p>



<ul class="wp-block-list">
<li><strong>Create:</strong> Generate new records.</li>



<li><strong>Read:</strong> Retrieve or view existing records.</li>



<li><strong>Update:</strong> Modify existing records.</li>



<li><strong>Delete:</strong> Remove or destroy records.</li>
</ul>



<p><strong>Prerequisite:</strong><br><a href="https://www.aiuniverse.xyz/how-to-install-composer-in-windows/">Composer</a><br><a href="https://www.aiuniverse.xyz/how-to-setup-xampp-in-windows-10/">MySQL</a><br>PHP >= 8.2</p>



<h2 class="wp-block-heading"><strong>Step 1: Install Laravel 11</strong></h2>



<p>First, choose a folder where you want to install Laravel. Then, run the following command in Terminal or CMD to install Laravel 11:</p>



<p><strong>Install via Composer:</strong></p>



<pre class="wp-block-code"><code>composer create-project laravel/laravel ajax-crud</code></pre>



<h2 class="wp-block-heading"><strong>Step 2: Configure Database Settings</strong></h2>



<p>Once the installation is complete, open the <code>.env</code> file and configure the database settings as follows:</p>



<pre class="wp-block-code"><code>DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name (e.g., crud_ajax)
DB_USERNAME=your_database_username (e.g., root)
DB_PASSWORD=your_database_password (e.g., root)</code></pre>



<h2 class="wp-block-heading">Step 3: Create Model and Database Migration</h2>



<ul class="wp-block-list">
<li><strong>Model</strong>: A class that represents a database table.</li>



<li><strong>Migration</strong>: Functions like version control for the database, enabling you to modify and share the database schema with your team.</li>
</ul>



<p>Run the following command in the Terminal or CMD to create both a model and a migration:</p>



<pre class="wp-block-code"><code>php artisan make:model Project --migration</code></pre>



<p>After executing this command, you&#8217;ll find a new file in the <code>database/migrations</code> directory. Add the code below to this migration file.</p>



<pre class="wp-block-code"><code>&lt;?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('projects');
    }
};</code></pre>



<p>Execute the migration by running the following Artisan command:</p>



<pre class="wp-block-code"><code>php artisan migrate</code></pre>



<h2 class="wp-block-heading"><strong>Step 4: Create a Resource Controller</strong></h2>



<p>A Resource Controller manages all HTTP requests related to a specific resource within your application.</p>



<p>To generate a Resource Controller, run the following command:</p>



<pre class="wp-block-code"><code>php artisan make:controller ProjectController --api</code></pre>



<p>This command will create a new controller file at <code><strong>app/Http/Controllers/ProjectController.php</strong></code>. The file will include methods for each of the standard resource operations. Open this file and add the necessary code as outlined below:</p>



<p><code><strong>app/Http/Controllers/ProjectController.php</strong></code></p>



<pre class="wp-block-code"><code>&lt;?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use App\Models\Project;
 
class ProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $projects = Project::get();
        return response()->json(&#91;'projects' => $projects]);
    }
 
    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        request()->validate(&#91;
            'name' => 'required|max:255',
            'description' => 'required',
        ]);
   
        $project = new Project();
        $project->name = $request->name;
        $project->description = $request->description;
        $project->save();
        return response()->json(&#91;'status' => "success"]);
    }
 
    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $project = Project::find($id);
        return response()->json(&#91;'project' => $project]);
    }
 
    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        request()->validate(&#91;
            'name' => 'required|max:255',
            'description' => 'required',
        ]);
   
        $project = Project::find($id);
        $project->name = $request->name;
        $project->description = $request->description;
        $project->save();
        return response()->json(&#91;'status' => "success"]);
    }
 
    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        Project::destroy($id);
        return response()->json(&#91;'status' => "success"]);
    }
}</code></pre>



<h2 class="wp-block-heading"><strong>Step 5: Register Routes</strong></h2>



<p>Once you have created the resource controller, you need to register both the resourceful route and the view route.</p>



<p><strong>In <code>routes/web.php</code>:</strong></p>



<pre class="wp-block-code"><code>&lt;?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProjectController;

Route::get('/', function () {
    return view('projects');
});

Route::apiResource('projects', ProjectController::class);</code></pre>



<h2 class="wp-block-heading">Step 6: Create a Blade File</h2>



<p>Blade is Laravel&#8217;s simple yet powerful templating engine. Blade view files have the <strong><code>.blade.php</code> </strong>extension and are typically stored in the <strong><code>resources/views/</code> directory</strong>.</p>



<p>To enhance the appearance of our simple CRUD app, we will use Bootstrap. Bootstrap is a popular, free, open-source CSS framework designed for building responsive and mobile-first web projects.</p>



<p>Create a Blade file named <code><strong>projects.blade.php</strong></code> in the<strong> <code>resources/views/</code> directory</strong>. After creating the file, insert the following code:</p>



<p><code><strong>resources/views/projects.blade.php</strong></code></p>



<pre class="wp-block-code"><code>&lt;!DOCTYPE html>
&lt;html lang="en">
&lt;head>
    &lt;title>Laravel Ajax CRUD&lt;/title>
    &lt;meta charset="utf-8">
    &lt;meta name="csrf-token" content="{{ csrf_token() }}">
    &lt;meta name="app-url" content="{{ url('/') }}">
    &lt;meta name="viewport" content="width=device-width, initial-scale=1">
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">&lt;/script>
    &lt;link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    &lt;script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous">&lt;/script>
    
&lt;/head>
&lt;body>
  
    &lt;div class="container">
        &lt;h2 class="text-center mt-5 mb-3">Laravel Ajax CRUD&lt;/h2>
        &lt;div class="card">
            &lt;div class="card-header">
                &lt;button class="btn btn-outline-primary" onclick="createProject()"> 
                    Add Product
                &lt;/button>
            &lt;/div>
            &lt;div class="card-body">
                &lt;div id="alert-div">
                 
                &lt;/div>
                &lt;table class="table table-bordered">
                    &lt;thead>
                        &lt;tr>
                            &lt;th>Product Name&lt;/th>
                            &lt;th>Product Description&lt;/th>
                            &lt;th width="240px">Action&lt;/th>
                        &lt;/tr>
                    &lt;/thead>
                    &lt;tbody id="projects-table-body">
                         
                    &lt;/tbody>
                     
                &lt;/table>
            &lt;/div>
        &lt;/div>
    &lt;/div>
  
    &lt;!-- modal for creating and editing function -->
    &lt;div class="modal" tabindex="-1"  id="form-modal">
        &lt;div class="modal-dialog" >
            &lt;div class="modal-content">
            &lt;div class="modal-header">
                &lt;h5 class="modal-title">Product Form&lt;/h5>
                &lt;button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">&lt;/button>
            &lt;/div>
            &lt;div class="modal-body">
                &lt;div id="error-div">&lt;/div>
                &lt;form>
                    &lt;input type="hidden" name="update_id" id="update_id">
                    &lt;div class="form-group">
                        &lt;label for="name">Product Name&lt;/label>
                        &lt;input type="text" class="form-control" id="name" name="name">
                    &lt;/div>
                    &lt;div class="form-group">
                        &lt;label for="description">Product Description&lt;/label>
                        &lt;textarea class="form-control" id="description" rows="3" name="description">&lt;/textarea>
                    &lt;/div>
                 
                    &lt;button type="submit" class="btn btn-outline-primary mt-3" id="save-project-btn">Save Product&lt;/button>
                &lt;/form>
            &lt;/div>
            &lt;/div>
        &lt;/div>
    &lt;/div>
 
  
    &lt;!-- view record modal -->
    &lt;div class="modal" tabindex="-1" id="view-modal">
        &lt;div class="modal-dialog" >
            &lt;div class="modal-content">
            &lt;div class="modal-header">
                &lt;h5 class="modal-title">Project Information&lt;/h5>
                &lt;button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">&lt;/button>
            &lt;/div>
            &lt;div class="modal-body">
                &lt;b>Name:&lt;/b>
                &lt;p id="name-info">&lt;/p>
                &lt;b>Description:&lt;/b>
                &lt;p id="description-info">&lt;/p>
            &lt;/div>
            &lt;/div>
        &lt;/div>
    &lt;/div>
  
    &lt;script type="text/javascript">
  
        showAllProjects();
     
        /*
            This function will get all the project records
        */
        function showAllProjects()
        {
            let url = $('meta&#91;name=app-url]').attr("content") + "/projects";
            $.ajax({
                url: url,
                type: "GET",
                success: function(response) {
                    $("#projects-table-body").html("");
                    let projects = response.projects;
                    for (var i = 0; i &lt; projects.length; i++) 
                    {
                        let showBtn =  '&lt;button ' +
                            ' class="btn btn-outline-info" ' +
                            ' onclick="showProject(' + projects&#91;i].id + ')">Show' +
                        '&lt;/button> ';
                        let editBtn =  '&lt;button ' +
                            ' class="btn btn-outline-success" ' +
                            ' onclick="editProject(' + projects&#91;i].id + ')">Edit' +
                        '&lt;/button> ';
                        let deleteBtn =  '&lt;button ' +
                            ' class="btn btn-outline-danger" ' +
                            ' onclick="destroyProject(' + projects&#91;i].id + ')">Delete' +
                        '&lt;/button>';
     
                        let projectRow = '&lt;tr>' +
                            '&lt;td>' + projects&#91;i].name + '&lt;/td>' +
                            '&lt;td>' + projects&#91;i].description + '&lt;/td>' +
                            '&lt;td>' + showBtn + editBtn + deleteBtn + '&lt;/td>' +
                        '&lt;/tr>';
                        $("#projects-table-body").append(projectRow);
                    }
     
                     
                },
                error: function(response) {
                    console.log(response.responseJSON)
                }
            });
        }
     
        /*
            check if form submitted is for creating or updating
        */
        $("#save-project-btn").click(function(event ){
            event.preventDefault();
            if($("#update_id").val() == null || $("#update_id").val() == "")
            {
                storeProject();
            } else {
                updateProject();
            }
        })
     
        /*
            show modal for creating a record and 
            empty the values of form and remove existing alerts
        */
        function createProject()
        {
            $("#alert-div").html("");
            $("#error-div").html("");   
            $("#update_id").val("");
            $("#name").val("");
            $("#description").val("");
            $("#form-modal").modal('show'); 
        }
     
        /*
            submit the form and will be stored to the database
        */
        function storeProject()
        {   
            $("#save-project-btn").prop('disabled', true);
            let url = $('meta&#91;name=app-url]').attr("content") + "/projects";
            let data = {
                name: $("#name").val(),
                description: $("#description").val(),
            };
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta&#91;name="csrf-token"]').attr('content')
                },
                url: url,
                type: "POST",
                data: data,
                success: function(response) {
                    $("#save-project-btn").prop('disabled', false);
                    let successHtml = '&lt;div class="alert alert-success" role="alert">&lt;b>Project Created Successfully&lt;/b>&lt;/div>';
                    $("#alert-div").html(successHtml);
                    $("#name").val("");
                    $("#description").val("");
                    showAllProjects();
                    $("#form-modal").modal('hide');
                },
                error: function(response) {
                    $("#save-project-btn").prop('disabled', false);
     
                    /*
        show validation error
                    */
                    if (typeof response.responseJSON.errors !== 'undefined') 
                    {
        let errors = response.responseJSON.errors;
        let descriptionValidation = "";
        if (typeof errors.description !== 'undefined') 
                        {
                            descriptionValidation = '&lt;li>' + errors.description&#91;0] + '&lt;/li>';
                        }
                        let nameValidation = "";
        if (typeof errors.name !== 'undefined') 
                        {
                            nameValidation = '&lt;li>' + errors.name&#91;0] + '&lt;/li>';
                        }
         
        let errorHtml = '&lt;div class="alert alert-danger" role="alert">' +
            '&lt;b>Validation Error!&lt;/b>' +
            '&lt;ul>' + nameValidation + descriptionValidation + '&lt;/ul>' +
        '&lt;/div>';
        $("#error-div").html(errorHtml);        
    }
                }
            });
        }
     
     
        /*
            edit record function
            it will get the existing value and show the Product Form
        */
        function editProject(id)
        {
            let url = $('meta&#91;name=app-url]').attr("content") + "/projects/" + id ;
            $.ajax({
                url: url,
                type: "GET",
                success: function(response) {
                    let project = response.project;
                    $("#alert-div").html("");
                    $("#error-div").html("");   
                    $("#update_id").val(project.id);
                    $("#name").val(project.name);
                    $("#description").val(project.description);
                    $("#form-modal").modal('show'); 
                },
                error: function(response) {
                    console.log(response.responseJSON)
                }
            });
        }
     
        /*
            sumbit the form and will update a record
        */
        function updateProject()
        {
            $("#save-project-btn").prop('disabled', true);
            let url = $('meta&#91;name=app-url]').attr("content") + "/projects/" + $("#update_id").val();
            let data = {
                id: $("#update_id").val(),
                name: $("#name").val(),
                description: $("#description").val(),
            };
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta&#91;name="csrf-token"]').attr('content')
                },
                url: url,
                type: "PUT",
                data: data,
                success: function(response) {
                    $("#save-project-btn").prop('disabled', false);
                    let successHtml = '&lt;div class="alert alert-success" role="alert">&lt;b>Project Updated Successfully&lt;/b>&lt;/div>';
                    $("#alert-div").html(successHtml);
                    $("#name").val("");
                    $("#description").val("");
                    showAllProjects();
                    $("#form-modal").modal('hide');
                },
                error: function(response) {
                    /*
        show validation error
                    */
                    $("#save-project-btn").prop('disabled', false);
                    if (typeof response.responseJSON.errors !== 'undefined') 
                    {
                        console.log(response)
        let errors = response.responseJSON.errors;
        let descriptionValidation = "";
        if (typeof errors.description !== 'undefined') 
                        {
                            descriptionValidation = '&lt;li>' + errors.description&#91;0] + '&lt;/li>';
                        }
                        let nameValidation = "";
        if (typeof errors.name !== 'undefined') 
                        {
                            nameValidation = '&lt;li>' + errors.name&#91;0] + '&lt;/li>';
                        }
         
        let errorHtml = '&lt;div class="alert alert-danger" role="alert">' +
            '&lt;b>Validation Error!&lt;/b>' +
            '&lt;ul>' + nameValidation + descriptionValidation + '&lt;/ul>' +
        '&lt;/div>';
        $("#error-div").html(errorHtml);        
    }
                }
            });
        }
     
        /*
            get and display the record info on modal
        */
        function showProject(id)
        {
            $("#name-info").html("");
            $("#description-info").html("");
            let url = $('meta&#91;name=app-url]').attr("content") + "/projects/" + id +"";
            $.ajax({
                url: url,
                type: "GET",
                success: function(response) {
                    let project = response.project;
                    $("#name-info").html(project.name);
    $("#description-info").html(project.description);
    $("#view-modal").modal('show'); 
     
                },
                error: function(response) {
                    console.log(response.responseJSON)
                }
            });
        }
     
        /*
            delete record function
        */
        function destroyProject(id)
        {
            let url = $('meta&#91;name=app-url]').attr("content") + "/projects/" + id;
            let data = {
                name: $("#name").val(),
                description: $("#description").val(),
            };
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta&#91;name="csrf-token"]').attr('content')
                },
                url: url,
                type: "DELETE",
                data: data,
                success: function(response) {
                    let successHtml = '&lt;div class="alert alert-success" role="alert">&lt;b>Project Deleted Successfully&lt;/b>&lt;/div>';
                    $("#alert-div").html(successHtml);
                    showAllProjects();
                },
                error: function(response) {
                    console.log(response.responseJSON)
                }
            });
        }
     
    &lt;/script>
&lt;/body>
&lt;/html></code></pre>



<h2 class="wp-block-heading"><strong>Step 7: Run the Application</strong></h2>



<p>Once you have completed the previous steps, start your application by executing the following command:</p>



<pre class="wp-block-code"><code>php artisan serve</code></pre>



<p>After the application is running, open your browser and navigate to:</p>



<pre class="wp-block-code"><code>http:&#47;&#47;localhost:8000</code></pre>



<h2 class="wp-block-heading">Screenshots:</h2>



<p><strong>Laravel 11 AJAX CRUD Application &#8211; Product Index Page:</strong></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="337" src="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-14-1024x337.png" alt="" class="wp-image-19074" srcset="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-14-1024x337.png 1024w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-14-300x99.png 300w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-14-768x253.png 768w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-14.png 1304w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p><strong>Laravel 11 AJAX CRUD Application: Product Creation Form Modal</strong></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="349" src="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-15-1024x349.png" alt="" class="wp-image-19075" srcset="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-15-1024x349.png 1024w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-15-300x102.png 300w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-15-768x262.png 768w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-15.png 1311w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p><strong>Laravel 11 AJAX CRUD App (edit product form modal):</strong></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="333" src="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-16-1024x333.png" alt="" class="wp-image-19076" srcset="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-16-1024x333.png 1024w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-16-300x98.png 300w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-16-768x250.png 768w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-16.png 1283w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p><strong>Laravel 11 AJAX CRUD Application: Show Product Information in a Modal</strong></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="350" src="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-17-1024x350.png" alt="" class="wp-image-19077" srcset="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-17-1024x350.png 1024w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-17-300x102.png 300w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-17-768x262.png 768w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-17.png 1329w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Thank you for following along with this blog post series! I hope you found the step-by-step guides and explanations helpful in building and understanding your Laravel AJAX CRUD application. Your dedication to learning and exploring new concepts is truly commendable. If you have any questions or need further assistance, please feel free to reach out. Keep coding and continue enhancing your skills. Happy developing!</p>
<p>The post <a href="https://www.aiuniverse.xyz/laravel-11-ajax-crud-tutorial-simple-step-by-step-guide/">Laravel 11 AJAX CRUD Tutorial: Simple Step-by-Step Guide</a> appeared first on <a href="https://www.aiuniverse.xyz">Artificial Intelligence</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.aiuniverse.xyz/laravel-11-ajax-crud-tutorial-simple-step-by-step-guide/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>A Comprehensive Guide to CRUD Operations in Laravel 11</title>
		<link>https://www.aiuniverse.xyz/a-comprehensive-guide-to-crud-operations-in-laravel-11/</link>
					<comments>https://www.aiuniverse.xyz/a-comprehensive-guide-to-crud-operations-in-laravel-11/#respond</comments>
		
		<dc:creator><![CDATA[Maruti Kr.]]></dc:creator>
		<pubDate>Mon, 05 Aug 2024 12:27:22 +0000</pubDate>
				<category><![CDATA[laravel]]></category>
		<category><![CDATA[CRUD functionality Laravel 11]]></category>
		<category><![CDATA[CRUD operations]]></category>
		<category><![CDATA[Laravel 11 application]]></category>
		<category><![CDATA[Laravel 11 blog post]]></category>
		<category><![CDATA[Laravel 11 CRUD example]]></category>
		<category><![CDATA[Laravel 11 development]]></category>
		<category><![CDATA[Laravel 11 tutorial]]></category>
		<category><![CDATA[Laravel CRUD guide]]></category>
		<category><![CDATA[Laravel CRUD operations]]></category>
		<category><![CDATA[separated by commas: ``` Laravel 11]]></category>
		<category><![CDATA[Sure! Here are 10 keywords related to Laravel 11 CRUD operations]]></category>
		<guid isPermaLink="false">https://www.aiuniverse.xyz/?p=19005</guid>

					<description><![CDATA[<p>Hi Everyone, In this post, you will learn how to perform CRUD operations in Laravel 11. We&#8217;ll guide you through building a CRUD application from scratch. This <a class="read-more-link" href="https://www.aiuniverse.xyz/a-comprehensive-guide-to-crud-operations-in-laravel-11/">Read More</a></p>
<p>The post <a href="https://www.aiuniverse.xyz/a-comprehensive-guide-to-crud-operations-in-laravel-11/">A Comprehensive Guide to CRUD Operations in Laravel 11</a> appeared first on <a href="https://www.aiuniverse.xyz">Artificial Intelligence</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="438" src="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-3-1024x438.png" alt="" class="wp-image-19006" srcset="https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-3-1024x438.png 1024w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-3-300x128.png 300w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-3-768x328.png 768w, https://www.aiuniverse.xyz/wp-content/uploads/2024/08/image-3.png 1217w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Hi Everyone,</p>



<p>In this post, you will learn how to perform CRUD operations in Laravel 11. We&#8217;ll guide you through building a CRUD application from scratch. This blog post is perfect for beginners looking to understand the basics of CRUD operations in Laravel 11.</p>



<p>In this example, we&#8217;ll create a product CRUD application using Laravel 11. We&#8217;ll start by creating a products table with Laravel migration, followed by setting up routes, controllers, views, and models for the product module. To design the user interface, we&#8217;ll use Bootstrap 5.</p>



<p>So, let&#8217;s get started with creating a CRUD operation in Laravel 11.</p>



<h3 class="wp-block-heading">Step 1: Install Laravel 11</h3>



<p>First, we&#8217;ll install the Laravel 11 application. If you already have a project set up, you can skip this step.</p>



<pre class="wp-block-code"><code>composer create-project laravel/laravel example-app</code></pre>



<h3 class="wp-block-heading">Step 2: Database Configuration</h3>



<p>Open the <code><strong>.env</strong></code> file and update the database connection details as shown below:</p>



<pre class="wp-block-code"><code>DB_CONNECTION=mysql
 DB_HOST=127.0.0.1
 DB_PORT=3306
 DB_DATABASE=laravel_crud_db
 DB_USERNAME=root
 DB_PASSWORD=</code></pre>



<h3 class="wp-block-heading">Step 3: Create Migration</h3>



<p>Create a <code>categories</code> table in your MySQL database using the Laravel Artisan command as follows:</p>



<pre class="wp-block-code"><code>php artisan make:migration create_categories_table --create=categories</code></pre>



<p>After running this command, a migration file will be generated at <code><strong>database/migrations/2024_00_00_000000_create_categories_table.php</strong></code>. You need to add the following code to this migration file to define the structure of the <code>categories</code> table.</p>



<pre class="wp-block-code"><code>&lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('categories', function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;string('name');
            $table-&gt;string('description');
            $table-&gt;boolean('status')-&gt;default(1)-&gt;comment('1=visible,0=hidden');
            $table-&gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('categories');
    }
};</code></pre>



<p>Now, let&#8217;s migrate our database using the following command:</p>



<pre class="wp-block-code"><code>php artisan migrate</code></pre>



<p><strong>Step 4: Create a Model Using the Following Command:</strong></p>



<pre class="wp-block-code"><code>php artisan make:model Category</code></pre>



<p>After running this command, you will find the <code><strong>Category.php</strong></code> file in the <code><strong>app/Models</strong></code> directory. Open this file and insert the following code:</p>



<pre class="wp-block-code"><code>&lt;?php

namespace  App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;

    protected $table = 'categories';

    protected $fillable = &#91;
        'name',
        'description',
        'status'
    ];
}</code></pre>



<p><strong>Step 5:</strong> To create a new controller named <code>CategoryController</code>, run the following command:</p>



<pre class="wp-block-code"><code>php artisan make:controller CategoryController --resource</code></pre>



<p>After the controller is created, navigate to the <code>CategoryController.php</code> file located at <code><strong>app/Http/Controllers/CategoryController.php</strong></code> and replace its contents with the code provided below:</p>



<pre class="wp-block-code"><code>&lt;?php

namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $categories = Category::paginate(10);
        return view('category.index', &#91;
            'categories' =&gt; $categories
        ]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('category.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request-&gt;validate(&#91;
            'name' =&gt; 'required|string|max:255',
            'description' =&gt; 'required|string|max:255',
            'status' =&gt; 'nullable',
        ]);

        Category::create(&#91;
            'name' =&gt; $request-&gt;name,
            'description' =&gt; $request-&gt;description,
            'status' =&gt; $request-&gt;status == true ? 1:0,
        ]);

        return redirect('/category')-&gt;with('status','Category Created Successfully');
    }

    /**
     * Display the specified resource.
     */
    public function show(Category $category)
    {
        return view('category.show', compact('category'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Category $category)
    {
        return view('category.edit', compact('category'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Category $category)
    {
        $request-&gt;validate(&#91;
            'name' =&gt; 'required|string|max:255',
            'description' =&gt; 'required|string|max:255',
            'status' =&gt; 'nullable',
        ]);

        $category-&gt;update(&#91;
            'name' =&gt; $request-&gt;name,
            'description' =&gt; $request-&gt;description,
            'status' =&gt; $request-&gt;status == true ? 1:0,
        ]);

        return redirect('/category')-&gt;with('status','Category Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Category $category)
    {
        $category-&gt;delete();
        return redirect('/category')-&gt;with('status','Category Deleted Successfully');
    }
}</code></pre>



<h3 class="wp-block-heading">Step 6: Add Resource Route</h3>



<p>Open the <code><strong>routes/web.php</strong></code> file and add the following route to handle resourceful actions for the <code>CategoryController</code>:</p>



<pre class="wp-block-code"><code>&lt;?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CategoryController;



Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})-&gt;middleware(&#91;'auth', 'verified'])-&gt;name('dashboard');

Route::middleware('auth')-&gt;group(function () {
    Route::get('/profile', &#91;ProfileController::class, 'edit'])-&gt;name('profile.edit');
    Route::patch('/profile', &#91;ProfileController::class, 'update'])-&gt;name('profile.update');
    Route::delete('/profile', &#91;ProfileController::class, 'destroy'])-&gt;name('profile.destroy');    
    Route::resource('/category', CategoryController::class);
});

require __DIR__.'/auth.php';
</code></pre>



<h3 class="wp-block-heading">Step 7: Create Blade View Files</h3>



<p>Navigate to the <strong><code>resources/views/</code> directory</strong>. Inside this directory, create a folder named <code>category</code>. Within the <code>category</code> folder, create the following Blade view files:</p>



<ol class="wp-block-list">
<li><code>index.blade.php</code></li>



<li><code>create.blade.php</code></li>



<li><code>edit.blade.php</code></li>



<li><code>show.blade.php</code></li>



<li><code>layout.blade.php</code></li>
</ol>



<p>Here’s how to create these files and add the necessary code:</p>



<ol class="wp-block-list">
<li><strong><code>resources/views/category/layout.blade.php</code></strong></li>
</ol>



<pre class="wp-block-code"><code>&lt;!doctype html&gt;
&lt;html lang="{{ str_replace('_', '-',  app()-&gt;getLocale()) }}"&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;

    &lt;!-- CSRF Token --&gt;
    &lt;meta name="csrf-token" content="{{ csrf_token() }}"&gt;

    &lt;title&gt;Laravel 11 CRUD  Application&lt;/title&gt;

    &lt;!-- Fonts --&gt;
    &lt;link rel="dns-prefetch" href="//fonts.gstatic.com"&gt;
    &lt;link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet"&gt;

    &lt;!-- Styles --&gt;
    &lt;link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous"&gt;

&lt;/head&gt;
&lt;body&gt;

    &lt;div class="container"&gt;
        @yield('content')
    &lt;/div&gt;

    &lt;script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"&gt;&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;</code></pre>



<ol class="wp-block-list" start="2">
<li><strong><code>resources/views/category/index.blade.php</code></strong></li>
</ol>



<pre class="wp-block-code"><code>@extends('category.layout')

@section('content')

    &lt;div class="container"&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-md-12"&gt;

                @session('status')
                &lt;div class="alert alert-success"&gt;
                    {{ session('status') }}
                &lt;/div&gt;
                @endsession

                &lt;div class="card"&gt;
                    &lt;div class="card-header"&gt;
                        &lt;h4&gt;Categories List
                            &lt;a href="{{ url('category/create') }}" class="btn btn-primary float-end"&gt;Add Category&lt;/a&gt;
                        &lt;/h4&gt;
                    &lt;/div&gt;
                    &lt;div class="card-body"&gt;
                        &lt;table class="table table-stiped table-bordered"&gt;
                            &lt;thead&gt;
                                &lt;tr&gt;
                                    &lt;th&gt;Id&lt;/th&gt;
                                    &lt;th&gt;Name&lt;/th&gt;
                                    &lt;th&gt;Description&lt;/th&gt;
                                    &lt;th&gt;Status&lt;/th&gt;
                                    &lt;th&gt;Action&lt;/th&gt;
                                &lt;/tr&gt;
                            &lt;/thead&gt;
                            &lt;tbody&gt;
                                @foreach ($categories as $category)
                                &lt;tr&gt;
                                    &lt;td&gt;{{ $category-&gt;id }}&lt;/td&gt;
                                    &lt;td&gt;{{ $category-&gt;name }}&lt;/td&gt;
                                    &lt;td&gt;{{ $category-&gt;description }}&lt;/td&gt;
                                    &lt;td&gt;{{ $category-&gt;status == 1 ? 'Visible':'Hidden' }}&lt;/td&gt;
                                    &lt;td&gt;
                                        &lt;a href="{{ route('category.edit', $category-&gt;id) }}" class="btn btn-success"&gt;Edit&lt;/a&gt;
                                        &lt;a href="{{ route('category.show', $category-&gt;id) }}" class="btn btn-info"&gt;Show&lt;/a&gt;


                                        &lt;form action="{{ route('category.destroy', $category-&gt;id) }}" method="POST" class="d-inline"&gt;
                                            @csrf
                                            @method('DELETE')
                                            &lt;button type="submit" class="btn btn-danger"&gt;Delete&lt;/button&gt;
                                        &lt;/form&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                                @endforeach
                            &lt;/tbody&gt;
                        &lt;/table&gt;

                        {{ $categories-&gt;links() }}</code></pre>



<ol class="wp-block-list" start="3">
<li><strong><code>resources/views/category/create.blade.php</code></strong></li>
</ol>



<pre class="wp-block-code"><code>@extends('category.layout')

@section('content')

    &lt;div class="container"&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-md-12"&gt;
                &lt;div class="card"&gt;
                    &lt;div class="card-header"&gt;
                        &lt;h4&gt;Create Category
                            &lt;a href="{{ url('category') }}" class="btn btn-danger float-end"&gt;Back&lt;/a&gt;
                        &lt;/h4&gt;
                    &lt;/div&gt;
                    &lt;div class="card-body"&gt;
                        &lt;form action="{{ route('category.store') }}" method="POST"&gt;
                            @csrf

                            &lt;div class="mb-3"&gt;
                                &lt;label&gt;Name&lt;/label&gt;
                                &lt;input type="text" name="name" class="form-control" /&gt;
                                @error('name') &lt;span class="text-danger"&gt;{{ $message }}&lt;/span&gt; @enderror
                            &lt;/div&gt;
                            &lt;div class="mb-3"&gt;
                                &lt;label&gt;Description&lt;/label&gt;
                                &lt;textarea name="description" rows="3" class="form-control"&gt;&lt;/textarea&gt;
                                @error('description') &lt;span class="text-danger"&gt;{{ $message }}&lt;/span&gt; @enderror
                            &lt;/div&gt;
                            &lt;div class="mb-3"&gt;
                                &lt;label&gt;Status&lt;/label&gt;
                                &lt;br/&gt;
                                &lt;input type="checkbox" name="status" checked style="width:30px;height:30px;" /&gt; Checked=visible, unchecked=hidden
                                @error('status') &lt;span class="text-danger"&gt;{{ $message }}&lt;/span&gt; @enderror
                            &lt;/div&gt;
                            &lt;div class="mb-3"&gt;
                                &lt;button type="submit" class="btn btn-primary"&gt;Save&lt;/button&gt;
                            &lt;/div&gt;

                        &lt;/form&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

@endsection</code></pre>



<ol class="wp-block-list" start="4">
<li><strong><code>resources/views/category/edit.blade.php</code></strong></li>
</ol>



<pre class="wp-block-code"><code>@extends('category.layout')

@section('content')

    &lt;div class="container"&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-md-12"&gt;
                &lt;div class="card"&gt;
                    &lt;div class="card-header"&gt;
                        &lt;h4&gt;Edit Category
                            &lt;a href="{{ url('category') }}" class="btn btn-danger float-end"&gt;Back&lt;/a&gt;
                        &lt;/h4&gt;
                    &lt;/div&gt;
                    &lt;div class="card-body"&gt;
                        &lt;form action="{{ route('category.update', $category-&gt;id) }}" method="POST"&gt;
                            @csrf
                            @method('PUT')

                            &lt;div class="mb-3"&gt;
                                &lt;label&gt;Name&lt;/label&gt;
                                &lt;input type="text" name="name" class="form-control" value="{{ $category-&gt;name }}" /&gt;
                                @error('name') &lt;span class="text-danger"&gt;{{ $message }}&lt;/span&gt; @enderror
                            &lt;/div&gt;
                            &lt;div class="mb-3"&gt;
                                &lt;label&gt;Description&lt;/label&gt;
                                &lt;textarea name="description" rows="3" class="form-control"&gt;{!! $category-&gt;description !!}&lt;/textarea&gt;
                                @error('description') &lt;span class="text-danger"&gt;{{ $message }}&lt;/span&gt; @enderror
                            &lt;/div&gt;
                            &lt;div class="mb-3"&gt;
                                &lt;label&gt;Status&lt;/label&gt;
                                &lt;br/&gt;
                                &lt;input type="checkbox" name="status" {{ $category-&gt;status == 1 ? 'checked':'' }} style="width:30px;height:30px;" /&gt; Checked=visible, unchecked=hidden
                                @error('status') &lt;span class="text-danger"&gt;{{ $message }}&lt;/span&gt; @enderror
                            &lt;/div&gt;
                            &lt;div class="mb-3"&gt;
                                &lt;button type="submit" class="btn btn-primary"&gt;Update&lt;/button&gt;
                            &lt;/div&gt;

                        &lt;/form&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

@endsection</code></pre>



<ol class="wp-block-list" start="5">
<li><strong><code>resources/views/category/show.blade.php</code></strong></li>
</ol>



<pre class="wp-block-code"><code>@extends('category.layout')

@section('content')

    &lt;div class="container"&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-md-12"&gt;
                &lt;div class="card"&gt;
                    &lt;div class="card-header"&gt;
                        &lt;h4&gt;Show Category Detail
                            &lt;a href="{{ url('category') }}" class="btn btn-danger float-end"&gt;Back&lt;/a&gt;
                        &lt;/h4&gt;
                    &lt;/div&gt;
                    &lt;div class="card-body"&gt;
                        &lt;div class="mb-3"&gt;
                            &lt;label&gt;Name&lt;/label&gt;
                            &lt;p&gt;
                                {{ $category-&gt;name }}
                            &lt;/p&gt;
                        &lt;/div&gt;
                        &lt;div class="mb-3"&gt;
                            &lt;label&gt;Description&lt;/label&gt;
                            &lt;p&gt;
                                {!! $category-&gt;description !!}
                            &lt;/p&gt;
                        &lt;/div&gt;
                        &lt;div class="mb-3"&gt;
                            &lt;label&gt;Status&lt;/label&gt;
                            &lt;br/&gt;
                            &lt;p&gt;
                                {{ $category-&gt;status == 1 ? 'checked':'' }}
                            &lt;/p&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

@endsection</code></pre>



<p><strong>Step 8: Add the Pagination class to <code>app/Providers/AppServiceProvider.php</code>. If it is already included, you can skip this step.</strong></p>



<pre class="wp-block-code"><code>&lt;?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //
    }
}</code></pre>



<p><strong>Step 9:</strong> Serve the application by running the following command:</p>



<pre class="wp-block-code"><code>php artisan serve</code></pre>



<p>Open your web browser, enter the provided URL, and check the app&#8217;s output:</p>



<pre class="wp-block-code"><code>http:&#47;&#47;127.0.0.1:8000/category</code></pre>



<p>That&#8217;s the complete information. I trust this will be helpful to you.</p>
<p>The post <a href="https://www.aiuniverse.xyz/a-comprehensive-guide-to-crud-operations-in-laravel-11/">A Comprehensive Guide to CRUD Operations in Laravel 11</a> appeared first on <a href="https://www.aiuniverse.xyz">Artificial Intelligence</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.aiuniverse.xyz/a-comprehensive-guide-to-crud-operations-in-laravel-11/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
