Php 是否仅通过Laravel收集特定字段?
如何使用Laravel集合仅返回特定字段 例如:Php 是否仅通过Laravel收集特定字段?,php,laravel,Php,Laravel,如何使用Laravel集合仅返回特定字段 例如: $stores = $this->stores->allStores(); 这将返回许多带有id、名称、描述、街道、城镇的商店 我只需要id,name 是否需要使用map或filter?您可以使用Laravel的方法获取如下特定字段: $stores = $this->stores->allStores()->pluck('id', 'name'); // Or if - To get an array in re
$stores = $this->stores->allStores();
这将返回许多带有id、名称、描述、街道、城镇的商店
我只需要id
,name
是否需要使用map
或filter
?您可以使用Laravel的方法获取如下特定字段:
$stores = $this->stores->allStores()->pluck('id', 'name');
// Or if - To get an array in result
$stores_arr = $this->stores->allStores()->pluck('id', 'name')->toArray();
希望这有帮助。您可以使用Laravel的方法获取如下特定字段:
$stores = $this->stores->allStores()->pluck('id', 'name');
// Or if - To get an array in result
$stores_arr = $this->stores->allStores()->pluck('id', 'name')->toArray();
希望这对您有所帮助。您可以使用pluck()
方法:
$stores->pluck('id', 'name');
您可以使用pluck()
方法:
$stores->pluck('id', 'name');
非常感谢。如果我想通过
map
或filter
更新你的答案,你能更新你的答案吗?@我会回来的,你不应该使用map
或filter
,因为它们在另一种情况下使用。简单一点,pluck()
正是为此而设计的。谢谢。如果我想通过map
或filter
更新你的答案,你能更新你的答案吗?@我会回来的,你不应该使用map
或filter
,因为它们在另一种情况下使用。保持简单,pluck()
正是为此而设计的。