Laravel Fetch Single Row Data from Database

Laravel get or fetch single row data from the database; Through this tutorial, i am going to show you how to get or fetch single row data from database in laravel.

Laravel Fetch Single Row Data from Database

Use the below given, laravel eloquent methods to get or fetch single row data from database:

  • Using Laravel find()
  • Using Laravel firstWhere()
  • Using laravel where() and first()

Using Laravel find()

Use the laravel find() method to get one row or single row data from database; as shown below with example:

public function index()
{
    $userID = 1;
    $user = User::find($userID);
    
    dd($user);
}

Using Laravel firstWhere()

Use the laravel firstWhere() method to get one row or single row data from database; as shown below with example:

public function index()
{
    $email = '[email protected]';
    $user = User::firstWhere('email', $email);
  
    dd($user);
}

Using laravel where() and first()

Use the laravel where() and first() method to get one row or single row data from database; as shown below with example:

public function index()
{
    $email = '[email protected]';
    $user = User::where('email', $email)->first();
  
    dd($user);
}

Recommended Laravel Tutorials

Leave a Comment