且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

雄辩的ORM laravel 5获取ID数组

更新时间:2023-11-26 10:14:16

您可以使用 lists() :

You could use lists() :

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

注意:如果您以Studly Case格式(例如Test)定义模型,效果更好.

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

您还可以使用 get() :

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


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

在新版本>= 5.2中, lists() 方法已被 弃用,现在您可以使用 pluck() 方法:

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

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

注意: 如果需要字符串,例如在刀片中,则可以使用不带 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');