How to Get Single Row from Database in Laravel?

Nov 13, 2021 . Admin



Hi All,

Today, in this example I will explain to you how to get a single row from a database in laravel 8 application, I will show an example of laravel 8 laravel getting a single record from the database.

Here, I will give you a full example of laravel getting a single row by id, laravel finding row by id, laravel finding row by email, laravel getting single record from the database, how to get single record from database in laravel so follow my all steps.

Example 1: Using find() Method
/**
 * Write code on Method
 *
 * @return response()
 */
public function index()
{
    $userID = 1;
    $user = User::find($userID);
    
    dd($user);
}
Example 2: Using firstWhere() Method
/**
 * Write code on Method
 *
 * @return response()
 */
public function index()
{
    $email = 'admin@gmail.com';
    $user = User::firstWhere('email', $email);
  
    dd($user);
}
Example 3: Using where() and first() Method
/**
 * Write code on Method
 *
 * @return response()
 */
public function index()
{
    $email = 'admin@gmail.com';
    $user = User::where('email', $email)->first();
  
    dd($user);
}
Example 4: Using first()
/**
 * Write code on Method
 *
 * @return response()
 */
public function index()
{
    $user = User::first();
  
    dd($user);
}
Example 5: Using take() and get()
/**
 * Write code on Method
 *
 * @return response()
 */
public function index()
{
    $user = User::select('*')->take(1)->get();
   
    dd($user);
}

It will help you...

#Laravel 8 #Laravel