How to select certain fields in Laravel Eloquent?


Laravel Eloquent provides many ways to select the fields that you need from the table. Here are a few examples that show how it can be done.

Example 1

The following is an example of this −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { $users = User::select('name', 'email')->where('id', 1)->get(); foreach ($users as $u) { echo $u->name."=>".$u->email; } } }

Output

The output of the above code is −

Siya Khan=>siya@gmail.com

The above example is equivalent to the SQL query given below −

SELECT name, email FROM users WHERE id=1;

The output of the above code is −

+-----------+----------------+
| name      |       email    |
+-----------+----------------+
| Siya Khan | siya@gmail.com |
+-----------+----------------+

Example 2

Another way to fetch the fields from a table are as shown below −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { $users = User::where('id', 1)->get(['name','email']); foreach ($users as $u) { echo $u->name."=>".$u->email; } } }

Output

The output of the above code is −

Siya Khan=>siya@gmail.com

Example 3

In below we have used all() method with the field we need from table. It will fetch all the data from the table with the field values.

The SQL query for it is −

SELECT name, email from USERS;
The example is as shown below −
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index() { $users = User::all('name','email'); foreach ($users as $u) { echo $u->name."=>".$u->email." "; } } }

Output

The output of the above code is −

Siya Khan=>siya@gmail.com
Heena Khan=>heena@gmail.com
Seema=>SZJj8osSc1@gmail.com
Neha Singh=>KBc9tUG3pp@gmail.com
d7jhMWRMQe=>gOklW2HTzB@gmail.com
k118AjAswp=>y9bFEVQlI2@gmail.com
7ZH2Vk0TAp=>gZGo5mg53G@gmail.com
w8QSXDIBVU=>PZMoXHjCDs@gmail.com
feO56tC0sO=>PpZTTyMoZw@gmail.com
MntJcvWT2L=>GgCQRP16oP@gmail.com
RANLQbtj44=>Npkx5T2U1F@gmail.com
bben5rsdVv=>ogjjwF8GC3@gmail.com

Example 4

Using the pluck() method to get the fields as shown below −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { $users = User::where('id', 1)->pluck('name', 'email')->all(); print_r($users); } }

Output

The output of the above code is −

Array ( [siya@gmail.com] => Siya Khan )

Example 5

Using find() method to get the fields that we need.

The syntax is −

Model::find(valuetobeserarched, ['field1', 'field2']);

The example is as shown below −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { $users = User::find(1, ['name', 'email']); print_r($users); } }

Output

The output of the above code is

attributes:protected] => Array(
   [name] => Siya Khan
   [email] => siya@gmail.com
)

Updated on: 30-Aug-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements