select specific column in laravel eloquent code example

Example 1: laravel eloquent get only field name

//Eloquent: Get specific columns (not all the row). Pluck returns an array.
Model::where('id', 1)->pluck('name', 'surname');
// If you only want to get the result value:
Model::where('id', 1)->value('name');

Example 2: eloquent get only some columns

Table::select('name','surname')->where('id', 1)->get();

Example 3: get specific columns using with() function in laravel eloquent

Post::with('user:id,username')->get();

Tags:

Sql Example