Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 2, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 2, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 2, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 2, 2025

      How Red Hat just quietly, radically transformed enterprise server Linux

      June 2, 2025

      OpenAI wants ChatGPT to be your ‘super assistant’ – what that means

      June 2, 2025

      The best Linux VPNs of 2025: Expert tested and reviewed

      June 2, 2025

      One of my favorite gaming PCs is 60% off right now

      June 2, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      `document.currentScript` is more useful than I thought.

      June 2, 2025
      Recent

      `document.currentScript` is more useful than I thought.

      June 2, 2025

      Adobe Sensei and GenAI in Practice for Enterprise CMS

      June 2, 2025

      Over The Air Updates for React Native Apps

      June 2, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      You can now open ChatGPT on Windows 11 with Win+C (if you change the Settings)

      June 2, 2025
      Recent

      You can now open ChatGPT on Windows 11 with Win+C (if you change the Settings)

      June 2, 2025

      Microsoft says Copilot can use location to change Outlook’s UI on Android

      June 2, 2025

      TempoMail — Command Line Temporary Email in Linux

      June 2, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»How to update the Data using Laravel 11

    How to update the Data using Laravel 11

    February 10, 2025

    In the previous tutorial, we learnt how to fetch/read data from a database using Laravel 11. The tutorial is about, How to update the data in a database using Laravel 11. To update data in Laravel 11, follow these steps:

    Files created for this tutorial

    fetchController.php (app/Http/Controllers/fetchController.php)

    fetch.blade.php (resources/views/fetch.blade.php)

    update.blade.php (resources/views/update.blade.php)

    updateController.php (app/Http/Controllers/updateController.php)

    web.php (routes/web.php)

    The User Data looks like before the update.

    Laravel fetch data

    Step 1: Create Controller

    fetchController.php (app/Http/Controllers/fetchController.php)

    The above controller was already created in the previous tutorial.

    Step 2:  Create the Fetch View

    Create a Blade template at resources/views/posts/fetch.blade.php :

    @if(session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif
    
            @if(session('error'))
            <div class="alert alert-danger">{{ session('error') }}</div>
            @endif
                <table class="table table-striped table-hover table-bordered">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Email id</th>
                            <th>Mobile No</th>
                            <th>Address</th>
                            <th>City</th>
                            <th>State</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($data as $id => $user )
                        <tr>
                            <td>1</td>
                            <td>{{ $user->firstName }}</td>
                            <td>{{ $user->lastName }}</td>
                            <td>{{ $user->emailId }}</td>
                            <td>{{ $user->mobileNumber }}</td>
                            <td>{{ $user->address }}</td>
                            <td>{{ $user->city }}</td>
                            <td>{{ $user->state }}</td>
                            <td>    <a class="btn btn-primary btn-sm" href="{{ route('update', $user->id) }}">Edit</a></td>
                        </tr>
                        @endforeach
                          
                    </tbody>
                </table>

    Step 3: Create the Edit View

    update.blade.php (resources/views/update.blade.php)

    <form method="post" action="{{ route('updateUser', ['id' => $user->id]) }}">
                  @csrf
            
                  <div class="row g-3">
                    <div class="col-md-6">
                      <label for="your-name" class="form-label">First Name</label>
                      <input type="text" class="form-control" id="firstName" name="firstName"  value="{{ $user->firstName }}">
                      <span class="text-danger">
                      @error('firstName')
                      {{ $message }}
                      @enderror
                      </span>
                    </div>
                
                    <div class="col-md-6">
                      <label for="your-surname" class="form-label">Last Name</label>
                      <input type="text" class="form-control" id="lastName" name="lastName"  value="{{ $user->lastName }}">
                      <span class="text-danger">
                        @error('lastName')
                        {{ $message }}
                        @enderror
                        </span>
                    </div>
                    <div class="col-md-6">
                      <label for="your-email" class="form-label"> Email ID</label>
                      <input type="email" class="form-control" id="emailId" name="emailId"  value="{{ $user->emailId }}">
                      <span class="text-danger">
                        @error('emailId')
                        {{ $message }}
                        @enderror
                        </span>
                    </div>
                    <div class="col-md-6">
                      <label for="your-subject" class="form-label">Mobile No</label>
                      <input type="text" class="form-control" id="mobileNumber" name="mobileNumber"  value="{{ $user->mobileNumber }}">
                      <span class="text-danger">
                        @error('mobileNumber')
                        {{ $message }}
                        @enderror
                        </span>
                    </div>
                    <div class="col-12">
                      <label for="your-message" class="form-label">Address</label>
                      <textarea class="form-control" id="address" name="address" rows="5" >{{ $user->address }}</textarea>
                      <span class="text-danger">
                        @error('address')
                        {{ $message }}
                        @enderror
                        </span>
                    </div>
                    <div class="col-md-6">
                        <label for="your-email" class="form-label"> City</label>
                        <input type="text" class="form-control" id="city" name="city"  value="{{ $user->city }}">
                        <span class="text-danger">
                          @error('city')
                          {{ $message }}
                          @enderror
                          </span>
                      </div>
                      <div class="col-md-6">
                        <label for="your-subject" class="form-label">State</label>
                        <input type="text" class="form-control" id="state" name="state"  value="{{ $user->state }}">
                        <span class="text-danger">
                          @error('state')
                          {{ $message }}
                          @enderror
                          </span>
                      </div>
    
    
                    <div class="col-12">
                      <div class="row">
                        <div class="col-md-6">
                          <button type="submit" class="btn btn-dark w-100 fw-bold" name="submit" >Update</button>
                        </div>
                      </div>

    Step 4: Create Controller

    php artisan make:controller updateController

    updateController.php (app/Http/Controllers/updateController.php)

    <?php
    namespace AppHttpControllers;
    use IlluminateSupportFacadesDB;
    use IlluminateHttpRequest;
    
    class updateController extends Controller
    {
    
        // Show Particular user
        public function showUser(string $id){
            $user=DB::table('tblusers')->find($id);
            return view('update',['user' => $user]);
        }
    
    // Update a particular record
    public function updateUser(Request $request, $id){
        // return $request;
         $user=DB::table('tblusers')
         ->where('id', $id)
         ->update([
            'firstName' => $request->firstName,
            'lastName' => $request->lastName,
            'emailId' => $request->emailId,
            'mobileNumber' => $request->mobileNumber,
            'address' => $request->address,
            'state' => $request->state,
            'city' => $request->city
     
         ]);
         if($user){
             return redirect()->route('fetch')->with('success', 'Data updated successfully.');
         } else{
            return redirect()->route('fetch')->with('error', 'No changes made');
         }
        }
    
    }

    Step 5: Define Routes

    <?php
    use IlluminateSupportFacadesRoute;
    use AppHttpControllersinsertController;
    use AppHttpControllersfetchController;
    
    // For Form View
    Route::get('/', function () {
        return view('form');
    })->name('form');
    
    // For Insert Data
    Route::post('/insertdata',[insertController::class,'insertdata'])->name('insertdata');
    //For Fetch/Read Data
    Route::get('/fetch',[fetchController::class,'showData'])->name('fetch');
    // For Feth particualr user record
    Route::get('/update/{id}',[updateController::class,'showUser'])->name('update');
    // For Update the particualr user record
    Route::post('/updateUser/{id}',[updateController::class,'updateUser'])->name('updateUser');

    How to run the Script

    1. Download the project zip file

    2. Extract the file and copy insert-app  folder

    3. Paste inside root directory (for xampp xampp/htdocs, for wamp wamp/www, for lamp var/www/Html)

    4.Open PHPMyAdmin (http://localhost/phpmyadmin)

    5. Create a database with the name  userdb

    6. Import userdb.sql file(given inside the zip package in SQL file folder)

    7. Run these command

    PS C :> cd xampp/htdocs/insert-app

    PS C:xampphtdocsinsert-app> php artisan serve

    8. After that open the browser run the script

    http://127.0.0.1:8000/fetch

    Download Full source code(Update/Edit Data Using Laravel11)
    Size: 27.5 MB
    Version: V 1.0
    Download Now!

    The post How to update the Data using Laravel 11 appeared first on PHPGurukul.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleMicrosoft still hasn’t fixed the KB5044284 bug in Windows 11, even though it said it was resolved
    Next Article How to fetch / read data into MySQL database using Laravel 11

    Related Posts

    Security

    Chrome Zero-Day Alert: CVE-2025-5419 Actively Exploited in the Wild

    June 2, 2025
    Security

    CISA Adds 5 Actively Exploited Vulnerabilities to KEV Catalog: ASUS Routers, Craft CMS, and ConnectWise Targeted

    June 2, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    The new KB5055615 for Windows 11 Beta Channel brings huge improvements for the 23H2 version

    Operating Systems

    This subscription-free smart ring with remarkable battery life isn’t from Oura or Samsung

    News & Updates

    Cybercriminals Deploy 100K+ Malware Android Apps to Steal OTP Codes

    Development

    CVE-2025-5082 – “WordPress WP Attachments Reflected Cross-Site Scripting Vulnerability”

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    AMD is creeping into gaming territory at CES 2025. Here’s why Intel should be worried

    January 7, 2025

    AMD chips abound in gaming laptops this year at CES. I chatted with AMD’s Chief…

    Unlock the power of data governance and no-code machine learning with Amazon SageMaker Canvas and Amazon DataZone

    August 21, 2024

    CVE-2025-43565 – ColdFusion Incorrect Authorization Arbitrary Code Execution

    May 13, 2025

    7 ways to get more out of your Bitwarden password manager

    January 16, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.