Laravel雄辩:返回数组键作为字段ID

时间:2022-10-25 09:25:33

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的ORM、我想要的行ID作为数组结果的关键,这将使事情变得更容易,当我想检查一些基于ID(使用array_key_exists)或做一些看起来ups(如果我需要一个特定的条目从结果数组)

Is there any way I can tell Eloquent to set the key to the fields ID? see image http://oi60.tinypic.com/kc02a8.jpg

是否有办法让我雄辩地设置字段ID的键?见图片http://oi60.tinypic.com/kc02a8.jpg

4 个解决方案

#1


53  

You can simply do

你可以简单的做

    Model::all()->keyBy('category_id');

Cheers :)

欢呼:)

#2


28  

Since you have an Eloquent Collection (a child class of the generic Collection class) you can use the getDictionary method. $collection->getDictionary() will give you an array of your Category objects keyed by their primary keys.

由于您有一个有说服力的集合(泛型集合类的子类),您可以使用getDictionary方法。$collection—>getDictionary()将为您提供按主键键键键键入的类别对象数组。

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.

如果您想要另一个集合而不是一个本机PHP数组,您可以使用$ Collection—>keyBy($property)。看起来您的主键是category_id,所以$collection->keyBy('category_id')。您可以使用该方法对任意属性进行键控,包括您可能编写的任何get mutators。

While getDictionary is unique to the Eloquent Collection extension, keyBy is available to all Laravel Collection objects. See the Laravel 4.2 API docs or Laravel 5.0 API docs.

虽然getDictionary对于有意义的集合扩展是惟一的,但是keyBy对所有Laravel集合对象都是可用的。请参阅Laravel 4.2 API文档或Laravel 5.0 API文档。

#3


5  

You have a Support\Collection/Database\Eloquent\Collection you can use the method lists('id') to return an array of the id of each of the models within the collection.

您有一个支持\收集/数据库\雄辩的收集,您可以使用方法列表('id')返回集合中每个模型的id的数组。

Then use array_combine to 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映射到相应模型的数组。

#4


1  

If you need id as the key, then rely on the Collection:

如果您需要id作为键,则依赖集合:

$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);

#1


53  

You can simply do

你可以简单的做

    Model::all()->keyBy('category_id');

Cheers :)

欢呼:)

#2


28  

Since you have an Eloquent Collection (a child class of the generic Collection class) you can use the getDictionary method. $collection->getDictionary() will give you an array of your Category objects keyed by their primary keys.

由于您有一个有说服力的集合(泛型集合类的子类),您可以使用getDictionary方法。$collection—>getDictionary()将为您提供按主键键键键键入的类别对象数组。

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.

如果您想要另一个集合而不是一个本机PHP数组,您可以使用$ Collection—>keyBy($property)。看起来您的主键是category_id,所以$collection->keyBy('category_id')。您可以使用该方法对任意属性进行键控,包括您可能编写的任何get mutators。

While getDictionary is unique to the Eloquent Collection extension, keyBy is available to all Laravel Collection objects. See the Laravel 4.2 API docs or Laravel 5.0 API docs.

虽然getDictionary对于有意义的集合扩展是惟一的,但是keyBy对所有Laravel集合对象都是可用的。请参阅Laravel 4.2 API文档或Laravel 5.0 API文档。

#3


5  

You have a Support\Collection/Database\Eloquent\Collection you can use the method lists('id') to return an array of the id of each of the models within the collection.

您有一个支持\收集/数据库\雄辩的收集,您可以使用方法列表('id')返回集合中每个模型的id的数组。

Then use array_combine to 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映射到相应模型的数组。

#4


1  

If you need id as the key, then rely on the Collection:

如果您需要id作为键,则依赖集合:

$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);