我有一个变量$ cart,用于存储产品的详细信息。我想从购物车中获得一个特定的字段。
dd($ cart)显示以下结果。
Cart {#437 ▼
+items: array:1 [▼
"airports_334_64" => array:4 [▼
"qty" => 1
"price" => 1600000
"duration" => 0
"item" => array:28 [▼
"id" => 64
"created_at" => "2017-05-29 10:24:22"
"updated_at" => "2017-05-29 10:26:51"
"title" => "Airport ad"
"price" => "120000"
"location" => "Airport T3"
"city" => "Delhi"
"state" => "Delhi"
"rank" => "12"
"landmark" => "abc"
"description" => "<p>new</p>"
"image" => "1496053462.jpg"
"references" => ""
"status" => "Available"
"display_options" => null
"light_option" => null
"airportnumber" => null
"discount" => "1"
"slug" => null
"reference_mail" => "chingkhei91@gmail.com"
"airports_id" => "64"
"area" => "arrival_check_in_hall"
"displayoption" => "backlit_panel"
"dimensions" => "7'10"x3'9""
"optionprice" => "1600000"
"units" => "8"
"ad_code" => ""
"variation_id" => 334
]
]
]
+totalQty: 1
+totalPrice: 1600000
}
我想获取reference_mail的值,即Cart-&gt; items-&gt; item-&gt; reference_mail
答案 0 :(得分:2)
使用Laravel提供的Collections:
$items = collect($cart->items);
$reference_mails = $items->map(function($item){
return $item['reference_mail'];
});
这将为您提供所有参考邮件的数组。 map
的行为描述为here
答案 1 :(得分:1)
如果你想访问它们,那么你应该迭代这样的项目。
foreach ($cart->items as $items) {
foreach ($items as $item) {
$email = $item['item']['reference_mail'];
}
}
如果您需要根据数组索引直接访问它,那么您可以这样做。
$email = $cart->items['airports_334_64']['item']['reference_mail'];
如果您需要reference_mail
列表,那么您可以在查询中执行此操作,而不是完成所有这些操作。如果您有一个集合,那么您可以使用pluck
方法来检索电子邮件列表。