Laravel developers tips

Helpful solutions for Laravel framework developers

Eloquent

Get model column value in Laravel and new whereKey method in Laravel 5.4

January 22, 2017 by Marcin Nabiałek / 4 Comments

In some cases we want to get Eloquent model single column value based on other column value. Let’s assume we have user id and we want to get his e-mail only. We can obviously do:

1
2
$user = User::find($id);
$email = $user->email;

or even shorter:

1
$email = User::find($id)->email;

but there is one big problem with this – what if user doesn’t exist? Obviously … [Read more…]

Posted in: Eloquent, Laravel Tagged: value, wherekey

Marcin Nabiałek

Marcin Nabiałek - Certified Laravel Developer
Marcin Nabiałek - Zend Certified PHP Engineer

Categories

  • Composer
  • Eloquent
  • Laravel
  • Laravel Blade
  • Laravel Dusk
  • Laravel packages
  • Mockery
  • PHP
  • PhpUnit
  • Request
  • Testing
  • Validation

Copyright © 2023 Marcin Nabiałek