gpt4 book ai didi

php - 即使未选择,Laravel 下拉列表也会获取所有选项

转载 作者:行者123 更新时间:2023-12-03 23:00:46 25 4
gpt4 key购买 nike

我有一个下拉菜单,可以在其中显示我的产品属性,并且无论我是否选择任何选项,它都会将所有选项添加到我的购物车所选产品下。

我需要的是只获取选定的选项,或者如果没有选择任何选项,则将其保留为空。

这是我的 Blade 代码:

<tbody>
@foreach($options as $optiontitle => $optioncollection)
<tr>
<td style="width: 150px;">{{ $optiontitle }}</td>
<td class="text-left">
<select name="attr" class="form-control">
<option value="">Select</option>
@foreach($optioncollection as $suboption)
<option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
@endforeach
</select>
</td>
</tr>
@endforeach
</tbody>

这是我的属性的 Controller 方法,我称之为选项

$options = $product->suboptions->mapToGroups(function ($item, $key) {
return [$item->option->title => $item];
});

这是我的购物车方法,事情发生时所有东西都会添加到购物车而不是仅选择。

public function addingItem(Request $request, $id)
{
$product = Product::where('id', $id)->firstOrFail();

$customAttributes = [];
if(!empty($product->suboptions)){
foreach($product->suboptions as $subs) {
array_push($customAttributes, [
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
]
]);
}
}
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => $request->input('quantity'),
'attributes' => $customAttributes,
));
Session::flash('success', 'This product added to your cart successfully.');
return redirect()->back();
}

更新

我需要获得的是选项titleprice(查看我的订单示例以了解我需要这些选项的原因和位置)

"[{\"id\":29,\"name\":\"effewf\",\"price\":24524,\"quantity\":1,\"attributes\":[{\"attr\":{\"label\":\"Gray\",\"price\":\"7000.00\"}},{\"attr\":{\"label\":\"Red\",\"price\":\"5000.00\"}},{\"attr\":{\"label\":\"15\\\"\",\"price\":\"500000.00\"}},{\"attr\":{\"label\":\"17\\\"\",\"price\":\"700000.00\"}},{\"attr\":{\"label\":\"22\\\"\",\"price\":\"900000.00\"}}],\"conditions\":[]}]"

正如您在我的 attributes 部分中看到的,我有嵌套数组,例如:

\"attributes\":[{\"attr\":{\"label\":\"Gray\",\"price\":\"7000.00\"}},....

这是我在函数循环中获取标签价格的地方。

如果我使用循环,例如:

foreach($request->attr as $subs) {
array_push($customAttributes, [
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
]
]);

我会得到这个错误,而不是我所拥有的:

Trying to get property of non-object

在这一行:

  'attr' => [
'label' => $subs->title,
'price' => $subs->price,
PS:如果我将 $customAttributes = $request->attr; 与我自己的循环(在顶部)一起使用,我将仅获得所选选项的 id ,这对我来说没有好处.

最佳答案

因为您调用了产品的 suboptions 关系,而不是从请求中获取它。您使用此 $product->suboptions 调用它,而不是从请求中获取它。

$customAttributes = $request->attr; // try to dd($customAttributes) so you can see it

更新:

您只需将 id 传递给您的选项

<select name="attr[]" class="form-control" multiple>
<option value="">Select</option>
@foreach($optioncollection as $suboption)
<option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
@endforeach
</select>

并在您的 Controller 中,获取它们并验证

public function addingItem(Request $request, $id)
{
$product = Product::findOrFail($id);

$customAttributes = [];
if (!empty($request->attr)) {
foreach ($request->attr as $sub) {
// You should fetch the price from the database NOT from the user
// request as it will be very vulnerable to attacks

// find the suboption
$sub = Suboption::find($sub); // Here I assume you have the Model
// for your Product's suboptions
if (!empty($sub->id)) {
array_push($customAttributes, [
'attr' => [
'label' => $sub->title,
'price' => $sub->price,
]
]);
}
}
}

Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => $request->input('quantity'),
'attributes' => $customAttributes,
));

Session::flash('success', 'This product added to your cart successfully.');
return redirect()->back();
}

关于php - 即使未选择,Laravel 下拉列表也会获取所有选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com