LARAVEL Tutorial


Laravel 7 Retrieve Data From Database


In the previous tutorial, you have learned how to insert data into a database table in laravel web application. 

In this retrieve/fetch data into database table laravel tutorial. You will learn how to retrieve / fetch data into a database table in laravel web applications. Also, learn how to display the fetched data.

Laravel Retrieve Data In Database Example

  • Create Routes
  • Create Fetch Method Controller
  • Create List Blade View
  • Start Developement Server

Create Routes

Now, open your project in a text editor and go to routes/web.php file and update the following routes into your file:

Route::get('list','PostController@index');

Create Data Fetch Method Controller

Next step, you need to create one method for fetch/retrieve data from the database table. So update the below method in your PostController.php:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator,Redirect,Response;
use App\Post;

class PostController extends Controller
{  

    public function index()
    {  
       $data['posts'] = Post::paginate(10);
       return view('list', $data);
    }

}

The PostController, index() method will fetch data into the database and pass the data to list.blade.php view file.

Create List Blade View

Next step, you need to create blade view file, so go resource/views and create a new file name list.blade.php and update the following code into your file:

<html>
   <head>
      <title>Laravel Retrieve Data from Database</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <meta name="csrf-token" content="{{ csrf_token() }}">
      <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>
      <div class="container mt-5">
         <div class="row">
            <a href="{{ url('insert-form') }}" class="btn btn-success mb-2">Add</a> 
              <br>
            <div class="col-md-12">
                     
             <table class="table table-bordered" id="laravel_crud">
              <thead>
                 <tr>
                    <th>Id</th>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Created at</th>
                    <td colspan="2">Action</td>
                 </tr>
              </thead>
              <tbody>
                 @foreach($posts as $post)
                 <tr>
                    <td>{{ $post->id }}</td>
                    <td>{{ $post->title }}</td>
                    <td>{{ $post->description }}</td>
                    <td>{{ date('Y-m-d', strtotime($post->created_at)) }}</td>
                    <td><a href="{{  url('edit/'.$post->id) }}" class="btn btn-primary">Edit</a></td>
                    <td>
                    <form action="{{  url('delete/'.$post->id) }}" method="get">
                     {{ csrf_field() }}
                     <button class="btn btn-danger" type="submit">Delete</button>
                   </form>
                   </td>
                 </tr>
                 @endforeach
              </tbody>
             </table>
             {!! $posts->links() !!}
 
            </div>
         </div>
      </div>
   </body>
</html>

Start development server

You can use the following command artisan command to start the development server:

php artisan serve

The final step, go to your browser and type the below-given URL into your browser:

http://localhost:8000/list

OR

http://localhost/project-name/public/list