Martin AJ Martin AJ - 1 year ago 66
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

$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
columns. How can I fix it?

Answer Source

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

$arr = Users::get(array('id', 'name', 'email', 'age'))->toArray();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download