php Eloquent ORM laravel 5 获取 id 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34308169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:54:35  来源:igfitidea点击:

Eloquent ORM laravel 5 Get Array of ids

phplaravellaravel-5eloquentmodel

提问by paranoid

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 )

回答by Zakaria Acharki

You could use lists():

你可以使用lists()

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

NOTE :Better if you define your models in Studly Caseformat, 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 deprecatedin the new versions >= 5.2, now you could use pluck()method instead :

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

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:

注意:如果您需要string,例如在Blade 中,您可以使用没有toArray()部分的函数,例如:

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

回答by George

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

从 a Collection,你可以做的另一种方式是:

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

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

这将返回一个索引数组,whereIn()例如,laravel 在查询中完全可以使用。

回答by Amir Bar

read about the lists() method

阅读 list() 方法

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

回答by Radames E. Hernandez

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

正确答案是方法lists,它非常简单,如下所示:

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

Regards!

问候!

回答by Mehmet Bütün

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

您可以使用all()方法而不是toArray()方法(请参阅更多:laravel 文档):

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

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

如果你需要一个string,你可以不带toArray()附件使用:

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

回答by Mahmoud Mostafa

Just an extra info, if you are using DB:

只是一个额外的信息,如果你使用DB

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

And if using Eloquent model:

如果使用 Eloquent 模型:

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

回答by aslamdoctor

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

虽然你已经标记了答案,但这是一个更简单的方法

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

回答by Narendra Ojha

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

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

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

Regards

问候