laravel get first record from collection 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: collection get first element laravel

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

Tags:

Php Example