雄辩的ORM laravel 5获取ID数组

时间:2022-10-25 08:23:57

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

我正在使用Eloquent ORM laravel 5.1,我想返回一个大于0的id数组,我的模型称为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 )

4 个解决方案

#1


94  

You could use lists() :

你可以使用lists():

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

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

注意:如果您以Studly Case格式定义模型,例如Test,则更好。


You could also use get() :

你也可以使用get():

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

UPDATE : (For versions >= 5.2)

更新:(对于版本> = 5.2)

The lists() method was deprecated in the new versions >= 5.2, now you could use pluck() method instead :

在新版本> = 5.2中不推荐使用lists()方法,现在你可以使用pluck()方法:

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

Hope this helps.

希望这可以帮助。

#2


4  

read about the lists() method

阅读有关lists()方法的内容

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

#3


3  

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!

问候!

#4


0  

You may also use all() method to get array of selected attributes.

您还可以使用all()方法获取所选属性的数组。

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

Regards

问候

#1


94  

You could use lists() :

你可以使用lists():

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

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

注意:如果您以Studly Case格式定义模型,例如Test,则更好。


You could also use get() :

你也可以使用get():

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

UPDATE : (For versions >= 5.2)

更新:(对于版本> = 5.2)

The lists() method was deprecated in the new versions >= 5.2, now you could use pluck() method instead :

在新版本> = 5.2中不推荐使用lists()方法,现在你可以使用pluck()方法:

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

Hope this helps.

希望这可以帮助。

#2


4  

read about the lists() method

阅读有关lists()方法的内容

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

#3


3  

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!

问候!

#4


0  

You may also use all() method to get array of selected attributes.

您还可以使用all()方法获取所选属性的数组。

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

Regards

问候