paranoid paranoid - 5 months ago 26
PHP Question

Eloquent ORM laravel 5.1 Get Array of ids

I'm using Eloquent ORM laravel 5.1, i want to return an array of ids greater than 0, My model called

test
.

I have tried :

$test=test::select('id')->where('id' ,'>' ,0)->get()->toarray();


It return :

Array ( [0] => Array ( [id] => 1 ) [1] => Array ( [id] => 2 ) )


But i want result to be in simple array like :

Array ( 1,2 )

Answer

Try the following code :

test::where('id' ,'>' ,0)->lists('id')->toArray();

NOTE : Better if you define your Models in Camel Case format, e.g Test.


UPDATE : (for version 5.2)

The lists method was deprecated in the new version 5.2 use pluck method instead :

test::where('id' ,'>' ,0)->pluck('id')->toArray();

Hope this helps.