laravel get first record code example

Example 1: laravel get first record

'First email'
    User::whereEmail($email)->first();
'First Record'
   User::first();

Example 2: how to get just the first row from a table in laravel

$user = User::whereEmail($email)->first();

Example 3: laravel eloquent get first

// return first row by id
$user = App\User::where('id',$id)->first();
// or return directly a field
$userId = App\User::where(...)->pluck('id');

Example 4: laravel query builder select first

$user = DB::table('users')->where('name', 'John')->first();

var_dump($user->name);

Example 5: collection get first element laravel

$first = $msgs->first(); // this does ->take(1) under the hood

Tags:

Sql Example