如何在 Laravel Eloquent Collection 数组之前添加键值对?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33536112/
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
How to prepend Laravel Eloquent Collection array with a key-value pair?
提问by Matt Komarnicki
I'm fetching something from database:
我正在从数据库中获取一些东西:
$foo = $this->fooRepository->all()->lists('name', 'id');
I get:
我得到:
Collection {#506 ▼
#items: array:9 [▼
"9c436867-afe9-4234-a849-253aea4f602c" => "aaa"
"d250102b-1370-40d0-99c5-7e5bfd0a15e4" => "sss"
"7342f212-083b-458d-8af8-24986bbb627d" => "ddd"
"029c53ce-dc16-49fd-8d83-9d8270d9ff37" => "fff"
"3add6a37-72e2-4054-853e-9ed8addbf3ea" => "ggg"
"28f5a9ac-014e-4f22-bda8-e2d5b1f48273" => "hhh"
"94fccb2c-d732-4369-9bf7-78925797e578" => "jjj"
"5b494994-93f0-406e-b420-aceb7b6111d7" => "kkk"
"22a7824a-c6eb-45e7-b9c5-e40c134e3ac8" => "lll"
]
}
Perfect. This collection is later passed into Form::select
to populate select / option dropdown.
完美的。此集合稍后会传入Form::select
以填充选择/选项下拉列表。
I would like to prepend this collection with another key-value pair where key will be empty string and value will contain text like "Choose something".
我想在这个集合前面加上另一个键值对,其中键将为空字符串,值将包含诸如“选择某物”之类的文本。
I can append:
我可以附加:
$foo[''] = 'Choose something…';
$foo[''] = 'Choose something…';
so I get
所以我得到
Collection {#506 ▼
#items: array:10 [▼
"9c436867-afe9-4234-a849-253aea4f602c" => "aaa"
"d250102b-1370-40d0-99c5-7e5bfd0a15e4" => "sss"
"7342f212-083b-458d-8af8-24986bbb627d" => "ddd"
"029c53ce-dc16-49fd-8d83-9d8270d9ff37" => "fff"
"3add6a37-72e2-4054-853e-9ed8addbf3ea" => "ggg"
"28f5a9ac-014e-4f22-bda8-e2d5b1f48273" => "hhh"
"94fccb2c-d732-4369-9bf7-78925797e578" => "jjj"
"5b494994-93f0-406e-b420-aceb7b6111d7" => "kkk"
"22a7824a-c6eb-45e7-b9c5-e40c134e3ac8" => "lll"
"" => "Choose something…"
]
}
but no idea how I can move it as the first element of the collection. I simply cannot use array_merge
because I'm dealing with instance of Illuminate\Database\Eloquent\Collection
rather than array so this answerwon't work.
但不知道如何将它作为集合的第一个元素移动。我根本无法使用,array_merge
因为我正在处理的实例Illuminate\Database\Eloquent\Collection
而不是数组,所以这个答案不起作用。
Any hints? Thanks.
任何提示?谢谢。
回答by Joseph Silber
$foo = ['' => 'Choose something…'] + $foo->all();
If your $foo
must be a collection instance, simply wrap it in a collection again:
如果您$foo
必须是一个集合实例,只需再次将其包装在一个集合中:
$foo = collect(['' => 'Choose something…'] + $foo->all());
I submitted a PR to laravelto allow you to pass a key to prepend
. If you're using Laravel 5.1.24 or newer, you can now do this:
我向 laravel 提交了一个 PR 以允许您将密钥传递给prepend
. 如果你使用的是 Laravel 5.1.24 或更新版本,你现在可以这样做:
$foo = $this->fooRepository->all()
->lists('name', 'id')
->prepend('Choose something…', '');
Later versions of Laravel have renamed the lists
method to pluck
. If you're using relatively modern version of Laravel, use pluck
instead:
Laravel 的后续版本将该lists
方法重命名为pluck
. 如果您使用的是相对现代的 Laravel 版本,请pluck
改用:
$foo = $this->fooRepository->all()
->pluck('name', 'id')
->prepend('Choose something…', '');
回答by EspadaV8
Try using ->prepend()
尝试使用 ->prepend()
http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Collection.html#method_prepend
$foo->prepend('Choose Something')
http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Collection.html#method_prepend
$foo->prepend('Choose Something')
This won't give the correct result. The index would be 0
instead of an empty string. Instead you could try something like this
这不会给出正确的结果。索引将0
代替空字符串。相反,你可以尝试这样的事情
$foo = $foo->reverse()->put('Choose Something')->reverse()
But using @Joseph Silber answer is probably better.
但是使用@Joseph Silber 的答案可能更好。