使用地图和包含的 Laravel 集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43711940/
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 collection using map and contains
提问by I'll-Be-Back
I am having problem with available
key in the map
collection.
我遇到available
了map
集合中的密钥问题。
The available
key use contains
method. It should return true if the value of product id in $unavailableProducts does not contain in $products ($value->product_id == $product->id)
该available
键的使用contains
方法。如果 $unavailableProducts 中的 product id 值不包含在 $products 中,则应返回 true($value->product_id == $product->id)
What did I do wrong?
我做错了什么?
$unavailableProducts = $this->unavailableProducts();
$products = $this->products->all();
$allProducts = $products->map(function ($product) use($unavailableProducts) {
return [
'id' => $product->id,
'title' => $product->title,
'available' => $unavailableProducts['result']->contains(function ($value, $key) use ($product) {
if ($value->product_id == $product->id) {
return false;
}
return true;
}),
];
});
回答by crabbly
First, make sure that $unavailableProductions['result']
is a collection.
首先,确保这$unavailableProductions['result']
是一个集合。
Second, change your contains
method like this:
其次,contains
像这样改变你的方法:
$unavailableProducts = $this->unavailableProducts();
$products = $this->products->all();
$allProducts = $products->map(function ($product) use($unavailableProducts) {
return [
'id' => $product->id,
'title' => $product->title,
'available' => $unavailableProducts['result']->contains('id', $product->id),
];
});
The $unavailableProducts['result']->contains('id', $product->id)
will determine if the $unaviableProductions['result']
collection has a key id
where the value is $product->id
在$unavailableProducts['result']->contains('id', $product->id)
将确定$unaviableProductions['result']
集合有一个关键id
当值$product->id
回答by king neo
You can try this approach
你可以试试这个方法
$policyPackageDetails = PolicyPackage::where('isactive', '=', 1)->where('id', '=', $pid)
->with('policy_package_details')
->select('id',
'title',
'subtitle',
'shorttitle',
)
->get();
$policyPackageDetails = $policyPackageDetails ->map(function ($item){
return collect([
'id' => $item->id,
'title' => $item->title,
'subtitle' => $item->subtitle,
'short_title' => $item->shorttitle,
'policy_package_details' => $item->policy_package_details->map(function ($details){
return [
'attribute_name' => $details->attname,
'attribute_value' => $details->attvalue,
];
}),
]);
});
here fetching data from relational models, remaining & filtering them, the output will be like
这里从关系模型中获取数据,保留并过滤它们,输出将类似于
[
{
"id": 61,
"title": " Health (Platinum)",
"subtitle": "for 1 year",
"short_title": "HL-SHP-1",
"policy_package_details": [
{
"attribute_name": "in",
"attribute_value": "180000"
},
{
"attribute_name": "out",
"attribute_value": "20000"
}
]
}
]