How to Get Single Row from Database in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hello,

This article will provide some of the most important example how to fetch single row from database in laravel. you'll learn laravel get single row by id. Here you will learn laravel find row by id. This article will give you simple example of laravel find row by email. Here, Creating a basic example of laravel get single record from database.

we can fetch single record from database in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

Here, i will give you very simple some examples to getting single records from database. so let's see following examples:

Example 1: Using find()

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$userID = 1;

$user = User::find($userID);

dd($user);

}

Example 2: Using firstWhere()

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$email = 'aatmaninfotech@gmail.com';

$user = User::firstWhere('email', $email);

dd($user);

}

Example 3: Using where() and first()

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$email = 'aatmaninfotech@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);

}

I hope it can help you...

Tags :
Shares