How to get single column value in laravel

There are lots of method available in laravel to get single value. You can get the single column value from an object collection using the pluck() method with first() method in Laravel after the where condition.

Answers 3
  • Get single column value using the pluck() method in Laravel

    $categoryName = Category::where('id', $id)->pluck('name')->first();
    echo $categoryName;
    
    1

    Pluck method helps you to get the single column value from from a given key in laravel as a string.

  • Get single column value using value() method in laravel

    $userEmail = App\Models\User::where('id', 1)->value('email');
    echo $userEmail;
    
    1

    You have to pass the table column name which you want to get. This method will return the email address of user which of user id 1.

  • Get the single column value after where condition with first() Laravel eloquent method

    App\Models\User::where('name', 'summer')->first(['email'])->email;
    
    1

    Output:

    [email protected]

    You can get the single column value from the table using the first() eloquent method after the where condition in Laravel.

  • Back to code snippet queries related laravel