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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 1, 2025

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

      June 1, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 1, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 1, 2025

      My top 5 must-play PC games for the second half of 2025 — Will they live up to the hype?

      June 1, 2025

      A week of hell with my Windows 11 PC really makes me appreciate the simplicity of Google’s Chromebook laptops

      June 1, 2025

      Elden Ring Nightreign Night Aspect: How to beat Heolstor the Nightlord, the final boss

      June 1, 2025

      New Xbox games launching this week, from June 2 through June 8 — Zenless Zone Zero finally comes to Xbox

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

      Student Record Android App using SQLite

      June 1, 2025
      Recent

      Student Record Android App using SQLite

      June 1, 2025

      When Array uses less memory than Uint8Array (in V8)

      June 1, 2025

      Laravel 12 Starter Kits: Definite Guide Which to Choose

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

      My top 5 must-play PC games for the second half of 2025 — Will they live up to the hype?

      June 1, 2025
      Recent

      My top 5 must-play PC games for the second half of 2025 — Will they live up to the hype?

      June 1, 2025

      A week of hell with my Windows 11 PC really makes me appreciate the simplicity of Google’s Chromebook laptops

      June 1, 2025

      Elden Ring Nightreign Night Aspect: How to beat Heolstor the Nightlord, the final boss

      June 1, 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)

    Hostinger

    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

    Artificial Intelligence

    Markus Buehler receives 2025 Washington Award

    June 1, 2025
    Artificial Intelligence

    LWiAI Podcast #201 – GPT 4.5, Sonnet 3.7, Grok 3, Phi 4

    June 1, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    10+ Best WordPress Block Themes for WooCommerce

    Learning Resources

    How To Secure Our Kali Linux System To Ensure Our Protection

    Development

    The RTX 5070 could be delayed until March, stealing some of AMD’s RDNA 4 spotlight

    News & Updates

    Laravel SpaceOCR: Parse Images and Multi-page PDFs in Laravel

    Development

    Highlights

    Development

    Universal Design in Pharmacies – Digital Accessibility

    February 6, 2025

    In today’s digital age, ensuring that online platforms are accessible to everyone is essential. Universal…

    Improving JavaScript Speed and Responsiveness

    May 3, 2024

    “Pretty” is in the eye of the beholder

    April 18, 2025

    Netherlands’ Eindhoven University Hit by Cyberattack, Network Shut Down

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

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