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','[email protected]');

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:

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:

      <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="" rel="stylesheet">
      <div class="container mt-5">
         <div class="row">
            <a href="{{ url('insert-form') }}" class="btn btn-success mb-2">Add</a> 
            <div class="col-md-12">
             <table class="table table-bordered" id="laravel_crud">
                    <th>Created at</th>
                    <td colspan="2">Action</td>
                 @foreach($posts as $post)
                    <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>
                    <form action="{{  url('delete/'.$post->id) }}" method="get">
                     {{ csrf_field() }}
                     <button class="btn btn-danger" type="submit">Delete</button>
             {!! $posts->links() !!}

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:





This tutorial idea has taken from the following urls: