Laravel fetch single row column example; In this tutorial, we will show you how to get a single row from the database in Laravel. Here you will learn Laravel to get a single row by id or find a row by id. The below example explains how to fetch a single row using id or email or you can use any other unique column.
Here are the below examples of how to retrieve a single row in laravel from the database.
Example 1: Using Find() Method
use App\Models\User;
public function index()
{
$userID = 1;
$user = User::find($userID);
dd($user);
}
Example 2: Where() & First()
use App\Models\User;
public function index()
{
$userID = 1;
$user = User::where('id', $userID)->first();
dd($user);
}
Example 3: using firstWhere() Collection
use App\Models\User;
public function index()
{
$userID = 1;
$user = User::firstWhere('id', $userID);
dd($user);
}
Example 4: Get Single Row Using Email
use App\Models\User;
public function index()
{
$email = 'admin@gmail.com';
$user = User::where('email', $email);
dd($user);
}
Example 5: get First Record Using first()
use App\Models\User;
public function index()
{
$user = User::first();
dd($user);
}
Example 6: Using take() and get()
use App\Models\User;
public function index()
{
$user = User::take(1)->get();
dd($user);
}
Hope these examples help you.