Martin AJ Martin AJ - 25 days ago 4
SQL Question

How can I select some columns of table?

Here is my code:

$arr = Users::all()->toArray();


It returns an array of all table's columns. But I don't need to all columns. I need to return only these columns:
'id', 'name', 'email', 'age'
. I've searched about that and I figured this out: (using
pluck()
function)


$arr = Users::pluck('id', 'name', 'email', 'age')->toArray();


But it doesn't return expected result. It returns this:

array:7 [▼
"John" => 1
"Peter" => 2
"Jack" => 3
"Ali" => 4
"Teresco" => 5
"Mark" => 6
"Barman" => 7
]


As you see, there isn't
email
and
age
columns. How can I fix it?

Answer

You can collect arrays of columns in get() like this:

$arr = Users::get(array('id', 'name', 'email', 'age'))->toArray();
Comments