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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 3, 2025

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

      June 3, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 3, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 3, 2025

      All the WWE 2K25 locker codes that are currently active

      June 3, 2025

      PSA: You don’t need to spend $400+ to upgrade your Xbox Series X|S storage

      June 3, 2025

      UK civil servants saved 24 minutes per day using Microsoft Copilot, saving two weeks each per year according to a new report

      June 3, 2025

      These solid-state fans will revolutionize cooling in our PCs and laptops

      June 3, 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

      Community News: Latest PECL Releases (06.03.2025)

      June 3, 2025
      Recent

      Community News: Latest PECL Releases (06.03.2025)

      June 3, 2025

      A Comprehensive Guide to Azure Firewall

      June 3, 2025

      Test Job Failures Precisely with Laravel’s assertFailedWith Method

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

      All the WWE 2K25 locker codes that are currently active

      June 3, 2025
      Recent

      All the WWE 2K25 locker codes that are currently active

      June 3, 2025

      PSA: You don’t need to spend $400+ to upgrade your Xbox Series X|S storage

      June 3, 2025

      UK civil servants saved 24 minutes per day using Microsoft Copilot, saving two weeks each per year according to a new report

      June 3, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»How to fetch / read data into MySQL database using Laravel 11

    How to fetch / read data into MySQL database using Laravel 11

    February 10, 2025

    In the previous tutorial, we learnt how to insert data into the database using Larave l1.In this tutorial, we will learn How to fetch/read data from MySQL database using Laravel 11.

    Minimum requirement: PHP 8.2 or higher is needed to run Laravel 11. 

    We already created a MySQL table in the previous tutorial. Table structure for table tblusers.

    CREATE TABLE `tblusers` (
      `id` int(11) NOT NULL,
      `firstName` varchar(255) DEFAULT NULL,
      `lastName` varchar(255) DEFAULT NULL,
      `emailId` varchar(255) DEFAULT NULL,
      `mobileNumber` bigint(11) DEFAULT NULL,
      `address` varchar(255) DEFAULT NULL,
      `state` varchar(255) DEFAULT NULL,
      `city` varchar(255) DEFAULT NULL,
      `postingDate` timestamp NULL DEFAULT current_timestamp()
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

    Step 1: Create a controller for data fetching.

    php artisan make:controller fetchController

    Add logic to fetch/read data in the controller (app/Http/Controllers/fetchController.php)

    <?php
    namespace AppHttpControllers;
    use IlluminateSupportFacadesDB;
    use IlluminateHttpRequest;
    
    class fetchController extends Controller
    {
        public function showData(){
            $users=DB::table('tblusers')
                     ->get();
            return view('fetch',['data' => $users]);
        }
    }

    Step 2: Create a blade template to display/show data.

    <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>
                        </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>
                        </tr>
                        @endforeach
                          
                    </tbody>
                </table>

    Step 3: Add the routes to the routes/web.php

    <?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');

    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(Fetch/Read Data Using Laravel11)
    Size: 27.5 MB
    Version: V 1.0
    Download Now!

    The post How to fetch / read data into MySQL database using Laravel 11 appeared first on PHPGurukul.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleHow to update the Data using Laravel 11
    Next Article This app makes using Ollama local AI on MacOS devices so easy

    Related Posts

    Security

    BitoPro Silent on $11.5M Hack: Investigator Uncovers Massive Crypto Theft

    June 3, 2025
    Security

    New Linux Vulnerabilities

    June 3, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Advancing AI trust with new responsible AI tools, capabilities, and resources

    Development

    Profanify

    Development

    CVE-2025-48063 – XWiki Remote Code Execution via Required Rights Bypass

    Common Vulnerabilities and Exposures (CVEs)

    Unlocking the Full Potential of Linux’s Most Versatile Search Tool

    Development

    Highlights

    News & Updates

    I’ve been gaming on Alienware’s sharpest OLED monitor, and 4K has never looked so smooth

    March 29, 2025

    Alienware’s 27-inch, 4K, 240Hz OLED gaming monitor brings the latest tech to a smaller size,…

    Capcom highlights Onimusha: Way of the Sword in its February showcase

    February 4, 2025

    Malicious npm Package Leverages Unicode Steganography, Google Calendar as C2 Dropper

    May 15, 2025

    Clario enhances the quality of the clinical trial documentation process with Amazon Bedrock

    April 15, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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