Introduction

Reading data from the database is important and the most commonly used operation of any application. Laravel provides a variety of ways to fetch records from the database.

The two main ways to fetch records from the database are the following:

  • Using Eloquent

  • Using DB facade

Using models to fetch records

Models with e­loquence are defined within the App namespace­. These models respond to tables in our database. The mode­l class includes various methods for retrie­ving, creating, updating, and deleting re­cords from the database. To fetch records from a table, there are several built-in functions that can be utilized. The most frequently used ones include get(), first(), and all().

Fetching all the records

By using the code snippet below, all the records are fetched from the users table:

Get hands-on with 1200+ tech skills courses.