php Laravel Eloquent:返回 Array 键作为字段 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26865877/
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
Laravel Eloquent: Return Array key as the fields ID
提问by Broshi
When I execute a query via Laravel's Eloquent ORM, I want to get the row ID as the Array result key, this will make things easier when I want to check something based on an ID (using array_key_exists) or do some look ups (if I need a specific entry from the result array)
当我通过 Laravel 的 Eloquent ORM 执行查询时,我想获取行 ID 作为 Array 结果键,当我想根据 ID 检查某些内容(使用 array_key_exists)或进行一些查找(如果我需要结果数组中的特定条目)
Is there any way I can tell Eloquent to set the key to the fields ID?

回答by Bhoj Raj Bhatta
You can simply do
你可以简单地做
Model::all()->keyBy('category_id');
Cheers :)
干杯:)
回答by tremby
Since you have an Eloquent Collection (a child classof the generic Collectionclass) you can use the getDictionarymethod. $collection->getDictionary()will give you an array of your Category objects keyed by their primary keys.
既然你有雄辩的集合(子类中的泛型Collection类),您可以使用getDictionary方法。$collection->getDictionary()将为您提供由其主键键控的 Category 对象数组。
If you wanted another Collection rather than a native PHP array, you could instead use $collection->keyBy($property). It looks like your primary key is category_id, so $collection->keyBy('category_id'). You can use that method to key by any arbitrary property, including any get mutators you may have written.
如果您想要另一个 Collection 而不是本机 PHP 数组,您可以使用$collection->keyBy($property). 看起来您的主键是category_id,所以$collection->keyBy('category_id'). 您可以使用该方法对任意属性进行键控,包括您可能编写的任何 get mutators。
While getDictionaryis unique to the Eloquent Collectionextension, keyByis available to all Laravel Collectionobjects. See the Laravel 4.2 API docsor Laravel 5.0 API docs.
虽然getDictionary是EloquentCollection扩展独有的,但keyBy可用于所有 LaravelCollection对象。请参阅Laravel 4.2 API 文档或Laravel 5.0 API 文档。
回答by Matt Burrow
You have a Support\Collection/Database\Eloquent\Collectionyou can use the method lists('id')to return an array of the id of each of the models within the collection.
您有一个Support\Collection/Database\Eloquent\Collection您可以使用该方法lists('id')返回集合中每个模型的 id 数组。
Then use array_combineto map the keys to the models. The result of which will be an array with the id mapped to their corresponding model.
然后使用array_combine将键映射到模型。其结果将是一个 id 映射到其相应模型的数组。
回答by George John
If you need id as the key, then rely on the Collection:
如果你需要 id 作为key,那么就依赖Collection:
$collection = Model::all();
$collection->getDictionary();
// returns:
array(
1 => object(Model) ( ... ),
2 => object(Model) ( ... ),
...
idN => object(Model) ( ... )
);
Otherwise, nice or not, you can do this:
否则,好与不好,你可以这样做:
$keys = $collection->lists('columnName');
$arrayOfModels = array_combine($keys, $collection->getDictionary);
回答by NULL pointer
pluck('label','id')on the get() output is what you're looking for in Laravel 5.8+, which gives you a collection, ready to be toArray()ed
pluck('label','id')在 get() 输出上是您在 Laravel 5.8+ 中寻找的内容,它为您提供了一个可供toArray()编辑的集合
eg:
例如:
$options = \App\DataChoice::where([ 'field_id' => $dataField->id , 'active' => true ])->get()->pluck('label','id')->toArray();
I had a similar challenge - I wanted to a PHP array to allow me to create a with an for each choice, with the 's value being the id auto increment column, and the displayed text being the value.
我遇到了类似的挑战 - 我想要一个 PHP 数组,允许我为每个选择创建一个 with , 的值是 id 自动递增列,显示的文本是值。

