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=>[email protected]

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 | [email protected] |
+-----------+----------------+

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=>[email protected]

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=>[email protected]
Heena Khan=>[email protected]
Seema=>[email protected]
Neha Singh=>[email protected]
d7jhMWRMQe=>[email protected]
k118AjAswp=>[email protected]
7ZH2Vk0TAp=>[email protected]
w8QSXDIBVU=>[email protected]
feO56tC0sO=>[email protected]
MntJcvWT2L=>[email protected]
RANLQbtj44=>[email protected]
bben5rsdVv=>[email protected]

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 ( [[email protected]] => 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] => [email protected]
)
Updated on: 2022-08-30T12:29:55+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements