How to get specific columns using Laravel eloquent methods

Updated at 07-Jan-2022, By samar

How to get specific columns using Laravel eloquent methods

Through many examples, we will learn how to resolve the "How to get specific columns using Laravel eloquent methods".

You can get specific columns using Laravel eloquent methods (all(), first(), find()) while getting the data from tables. You have to specify the columns name in array to the eloquent methods.
  • Get specific column using Laravel all() eloquent method

    App\Models\User::all(['id', 'name', 'email']);
    

    You can get all users' records from the users table with column id, name and email.

  • Get specific columns using get() eloquent method in Laravel

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

    You can get all users' records from the users table using where condition with values of specified columns (id, name and email).

Back to code snippet queries related laravel