How To Fetch/Retrieve data and records From MySQL Database in Laravel 9


Laravel 9 Fetch Data From Database

Learn How To Fetch or Retrieve data and records From mysql database in Laravel 9 Framework. In this blog post We will explain its process step by step to you with the complete source code. We will show the fetched data from the database in the form of a table. And we have used Bootstrap for designing the table, you can also style it by yourself.

First you have to give database connection. To connect to the database, it is necessary that you go to the file named .env and give the name of your database.


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

You need to have the model and controller on your project to fetch the data.

After this you have to go to the web.php file and on that you have to create a route to call the function in your controller.
Path: routes/web.php

<?php

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

Route::get('/get_users', [FormController::class, 'get_users'] )->name("get_users");

Here we have commented the first line inside the function that's because we have shown the data here in the form of Pagination. If you do not want the data to appear in the form of pagination, then you can comment this method and uncomment the line above it.

Learn how to create a contact us form in Laravel 9

But there is an advantage of using the paginate method the only argument passed to the paginate method is the number of records you would like displayed "per page". In this case, let's specify that we would like to display 2 records per page:
Path: app/Http/Controllers/FormController.php


<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function get_users(){
        // $users=User::all();
        $users=User::orderBy('id','desc')->paginate(2);

        return view("get_users",['users'=>$users]);
    }
}

And after completing all this, to show the data, we will need a new file named as get_users.blade.php which will be of the same name as the route we have created (get_users).
Path: resources/views/get_users.blade.php

@include("components.htmlstartplate")
@include("components.navbar")

<h1 class="text-center">This is view (BYTEWEBSTER)</h1>
 <div class="container mx-auto text-center m-5 p-2">
        <div class="row">
           <div class="col-md-12 mx-auto">
                <table class="table table-striped table-hover border rounded">
                    <thead>
                        <tr>
                        <th scope="col"><h3>Name</h3></th>
                        <th scope="col"><h3>E-Mail</h3></th>
                        <th scope="col"><h3>Age</h3></th>
                        <th scope="col"><h3>Contact</h3></th>
                        <th scope="col"><h3>Gender</h3></th>
                        <th scope="col"><h3>Hobby</h3></th>
                        <th scope="col"><h3>Password</h3></th>
                        <th scope="col"><h3>Action</h3></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($users as $user)
                        <tr>
                        <td>{{ $user['name']}}</td>
                        <td>{{ $user['email']}}</td>
                        <td>{{ $user['age']}}</td>
                        <td>{{ $user['phone']}}</td>
                        <td>{{ $user['gender']}}</td>
                        <td>{{ $user['hobby']}}</td>
                        <td>{{ $user['password']}}</td>          
                        </tr>
                        @endforeach
                    </tbody>
               </table>

               <div>{{$users->links("pagination::bootstrap-5")}}</div>
            </div>
        </div>
    </div>
    
@include("components.htmlendplate")

in this code You can see @include if you don't know what it is then let me tell you @include is just like a basic PHP include_once() function, it includes a "partial" view into your view.
Example: @include("components.navbar")


Thank you for taking your valuable time to read this article. we hope you enjoyed it.

Connect With Us

we would like to keep in touch with you..... Register Here.

JOIN US JOIN TELEGRAM