114

I'm using Eloquent ORM laravel 5.1, and I want to return an array of ids greater than 0, my model is called test.

I have tried :

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

It returns :

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

But I want the result to be in simple array like this:

Array ( 1,2 )

9 Answers 9

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

NOTE: If you need a string, for example in a blade, you can use function without the toArray() part, like:

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

UPDATE: For versions < 5.2

You could use lists() :

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

NOTE : Better if you define your models in Studly Case format, e.g Test.


You could also use get() :

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

4
  • 3
    with pluck('id') array is array('0'=>12, '1'=>14) and etc, when used in WhereIn('id', $array), it selects not by id, but by array key, so by 0,1... Jul 16, 2018 at 9:02
  • 3
    toArray() should return an array like [12,14] Jul 16, 2018 at 9:06
  • 1
    oh yes, you are right, I was debugging via Debugbar ant print_r, and both of them showed array values with keys, but there are no keys. Thank you! Jul 16, 2018 at 11:19
  • We're stuck on 4.2 for one project, so I appreciate keeping the ->lists('id') reference. Although that generated an array directly, didn't need to ->toArray it. Jan 30, 2020 at 17:55
35

From a Collection, another way you could do it would be:

$collection->pluck('id')->toArray()

This will return an indexed array, perfectly usable by laravel in a whereIn() query, for instance.

2
  • 2
    Used for Drop Down list as well.
    – Bira
    Feb 12, 2019 at 1:34
  • To get the collection to pluck from from model \YourModel::all(['id']) ... ->pluck... (with specifying just the ID column you don't load all data to model)
    – jave.web
    May 21, 2019 at 10:58
7

The correct answer to that is the method lists, it's very simple like this:

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

Regards!

2
  • How would you get the related id's list in array through a many to many relationship??
    – Pathros
    Jan 30, 2018 at 17:09
  • Maybe you can use DB class, example: DB::table('name_of_table')->where('condition')->lists('id'); Feb 6, 2018 at 15:20
6

You can use all() method instead of toArray() method (see more: laravel documentation):

test::where('id' ,'>' ,0)->pluck('id')->all(); //returns array

If you need a string, you can use without toArray() attachment:

test::where('id' ,'>' ,0)->pluck('id'); //returns string
6

Just an extra info, if you are using DB:

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

And if using Eloquent model:

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

A simple way to get an array with the model IDs from a collection:

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

Available since Laravel 5.5: https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Collection.html#method_modelKeys

2
  • 2
    As for Laravel 8: ... ->get()->modelKeys()
    – Pathros
    Nov 5, 2021 at 1:19
  • ->get('id')->modelKeys(); - no need, it's enough ->->get()->model Keys(); Dec 3, 2021 at 7:47
5

read about the lists() method

$test=test::select('id')->where('id' ,'>' ,0)->lists('id')->toArray()
2
  • when I use in_array_command in blade file show this error. >in_array() expects parameter 2 to be array, object given
    – paranoid
    Dec 16, 2015 at 9:29
  • oh now I get you, you need to call toArray(), lists() return collection
    – Amir Bar
    Dec 16, 2015 at 9:32
2

Although you have marked the Answer, This is a much simpler approach

App\User::pluck('id')->toArray()
1

In Laravel 8 this works for me

$arr = SomeModel::where("field", value)->get()->toArray();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.