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 )
From a collection, another way you could do it would be:
This will return an indexed array, perfectly usable by laravel in a whereIn query, for instance.
The correct answer to that is the method
lists
, it's very simple like this:Regards!
read about the lists() method
You could use
lists()
:NOTE : Better if you define your models in
Studly Case
format, e.gTest
.You could also use
get()
:UPDATE: (For versions >= 5.2)
The
lists()
method was deprecated in the new versions>= 5.2
, now you could usepluck()
method instead :You may also use all() method to get array of selected attributes.
Regards