How to select specific columns in laravel eloquent

devnull Ψ picture devnull Ψ · Jul 3, 2016 · Viewed 360.3k times · Source

lets say I have 7 columns in table, and I want to select only two of them, something like this

SELECT `name`,`surname` FROM `table` WHERE `id` = '1';

In laravel eloquent model it may looks like this

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

but I guess this expression will select ALL columns where id equals 1, and I want only two columns(name, surname). how to select only two columns?

Answer

Marcin Nabiałek picture Marcin Nabiałek · Jul 3, 2016

You can do it like this:

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