Laravel Eloquent Pluck 不会丢失钥匙

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42355226/
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-09-14 15:22:43  来源:igfitidea点击:

Laravel Eloquent Pluck without losing the key

phplaraveleloquent

提问by Coder

I have the following collection in Laravel:

我在 Laravel 中有以下集合:

 ["TheNumbers":[{"episodeID":16818,"episodeNumber":100,"created_at":null,"updated_at":null},{"episodeID":16818,"episodeNumber":210,"created_at":"2017-02-20 21:30:38","updated_at":"2017-02-20 21:30:38"}]

If I run the following code:

如果我运行以下代码:

$TheEpisode->TheNumbers->pluck('episodeNumber');

I will get the following result:

我会得到以下结果:

[100,210]

I would like to keep the keys for each number, how can I achieve this?

我想保留每个号码的密钥,我怎样才能做到这一点?

EDIT: EXPECTED RESULT:

编辑:预期结果:

This is my expected result:

这是我的预期结果:

[{"episodeNumber":100},{"episodeNumber":210}]

PHILIS PETERS (improved)

菲利斯·彼得斯(改进版)

TheEpisode->TheNumbers->reduce(function ($result, $episode) {
          $episodeData = (collect())->put('episodeNumber', $episode['episodeNumber']);
          $result[] = $episodeData;
          return $result;
        }));

采纳答案by Mike Barwick

Try something like this, it should work using map...

尝试这样的事情,它应该可以使用map...

return $TheEpisode->TheNumbers->map(function ($episode) {
    return ['episodeNumber' => $episode['episodeNumber']];
});

回答by Rwd

Pluckcan take two params. The second of which is what the value can be keyed by.

Pluck可以带两个参数。第二个是值可以通过什么键控。

You should be able to do:

你应该能够做到:

$TheEpisode->TheNumbers->pluck('episodeNumber', 'episodeID');

Hope this helps!

希望这可以帮助!

回答by shxfee

This can be simply achieved by passing a second argument to pluck. From the documentation:

这可以通过向pluck传递第二个参数来简单地实现。从文档:

You may also specify how you wish the resulting collection to be keyed:

您还可以指定您希望如何对结果集合进行键控:

$plucked = $collection->pluck('name', 'product_id');

$plucked->all();

// ['prod-100' => 'Desk', 'prod-200' => 'Chair']

回答by wujt

$collection->forget(['created_at', 'updated_at]);

This will simply left two first key-value pairs. Worth to keep in mind:

这将只留下两个第一个键值对。值得牢记:

forget does not return a new modified collection; it modifies the collection it is called on.

忘记不会返回一个新的修改过的集合;它修改它被调用的集合。

Laravel docs

Laravel 文档

This should work properly:

这应该可以正常工作:

$collection->only(['episodeID', 'episodeNumber']);

回答by luke glenn jordan

Try using

尝试使用

$TheEpisode->TheNumbers->lists('episodeNumber');

the key will remain.

钥匙将保留。

回答by Phillis Peters

Try using mapWithKeyslike this:

尝试使用mapWithKeys这样的:

$TheEpisode->TheNumbers->mapWithKeys(function ($theNumber) {
return ['episodeNumber' => $theNumber['episodeNumber']
});

I assume you want the result like this:

我假设你想要这样的结果:

"TheNumbers":['episodeNumber':100,'episodeNumber':210]

"TheNumbers":['episodeNumber':100,'episodeNumber':210]

回答by flakerimi

For sake of updated Laravel, I tried all and I found

为了更新 Laravel,我尝试了所有,我发现

return Model::get(['name','id'])

return Model::get(['name','id'])

will give reduced results with key => value.

将使用 key => value 减少结果。