Laravel 即使未选中,下拉菜单也会获取所有选项
Laravel dropdown gets all options even if not selected
我有 drop-down 显示我的产品属性,无论我 select 是否有任何选项,它都会将所有选项添加到我的购物车 在 selected 产品下。
我需要的是获得 selected 选项,如果没有 selected,则将其留空。
这是我的 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>
这是我的 attributes
控制器方法,我称之为 option
$options = $product->suboptions->mapToGroups(function ($item, $key) {
return [$item->option->title => $item];
});
这是我的购物车方法,所有东西都将添加到购物车,而不是仅 selected。
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();
}
更新
我需要的是选项 title
和 price
(查看我的订单样本,了解我需要这些的原因和位置)
"[{\"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\"}},....
这里是我在函数循环中得到 label
和 price
的地方。
如果我使用这样的循环:
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
的 selected 选项,这对我不好。
您不应该从订单或请求中获取选项而不是从产品中获取选项吗?!
因为产品显然应该有所有选项。
............
-编辑:1
像这样改变你的循环
foreach($request->attr as $subs)
并将您的 selct 标签更改为如下所示
<select multiple>
-编辑2
编辑完你的问题后,我认为你有两个选择:
- 使用每个 $subs 的 id 从 DB 中再次查询您的子选项
或
在前端 html 选项标签中序列化标题和价格,这样你最终会得到这样的东西:
<option value="{{$suboption->title.','.$suboption->price}}" .....> .....</option>
然后在你的循环中这样做:
foreach($request->attr as $subs) {
$title_and_price = explode(',', $subs);
array_push($customAttributes, [
'attr' => [
'label' => $title_and_price[0],
'price' => $title_and_price[1]
]
]);
-编辑:3
我们还应该在 select 标签中的名称中添加方括号,如下所示:
<select name="{{ $optiontitle }}[]" multiple>
编辑:4 我发现另一个问题:
你应该在 select 中使用 name = "{{ $optiontitle }}[]" 因为它们不能都具有相同的名称因此,你必须使 [=52= 的 name 属性] 标签也是动态的
因为您调用了产品的 suboptions
关系,而不是从请求中获取。
您正在使用此 $product->suboptions
调用它,而不是从请求中获取它。
$customAttributes = $request->attr; // try to dd($customAttributes) so you can see it
更新:
你只需要将id传给你的options
<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>
然后在您的控制器中获取它们并验证
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();
}
尝试用下面的
替换控制器函数addingItem
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' => $request->input('attr'), // Attributes Array
));
Session::flash('success', 'This product added to your cart successfully.');
return redirect()->back();
}
我假设您需要通过
插入多个属性
@foreach($options as $optiontitle => $optioncollection)
替换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>
以下
<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>
它将创建一个属性数组来防止错误,
Trying to get property of non-object
如果您需要进一步的帮助,请发表评论。
我有 drop-down 显示我的产品属性,无论我 select 是否有任何选项,它都会将所有选项添加到我的购物车 在 selected 产品下。
我需要的是获得 selected 选项,如果没有 selected,则将其留空。
这是我的 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>
这是我的 attributes
控制器方法,我称之为 option
$options = $product->suboptions->mapToGroups(function ($item, $key) {
return [$item->option->title => $item];
});
这是我的购物车方法,所有东西都将添加到购物车,而不是仅 selected。
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();
}
更新
我需要的是选项 title
和 price
(查看我的订单样本,了解我需要这些的原因和位置)
"[{\"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\"}},....
这里是我在函数循环中得到 label
和 price
的地方。
如果我使用这样的循环:
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
的 selected 选项,这对我不好。
您不应该从订单或请求中获取选项而不是从产品中获取选项吗?!
因为产品显然应该有所有选项。
............
-编辑:1
像这样改变你的循环
foreach($request->attr as $subs)
并将您的 selct 标签更改为如下所示
<select multiple>
-编辑2
编辑完你的问题后,我认为你有两个选择:
- 使用每个 $subs 的 id 从 DB 中再次查询您的子选项
或
在前端 html 选项标签中序列化标题和价格,这样你最终会得到这样的东西:
<option value="{{$suboption->title.','.$suboption->price}}" .....> .....</option>
然后在你的循环中这样做:
foreach($request->attr as $subs) {
$title_and_price = explode(',', $subs);
array_push($customAttributes, [
'attr' => [
'label' => $title_and_price[0],
'price' => $title_and_price[1]
]
]);
-编辑:3
我们还应该在 select 标签中的名称中添加方括号,如下所示:
<select name="{{ $optiontitle }}[]" multiple>
编辑:4 我发现另一个问题:
你应该在 select 中使用 name = "{{ $optiontitle }}[]" 因为它们不能都具有相同的名称因此,你必须使 [=52= 的 name 属性] 标签也是动态的
因为您调用了产品的 suboptions
关系,而不是从请求中获取。
您正在使用此 $product->suboptions
调用它,而不是从请求中获取它。
$customAttributes = $request->attr; // try to dd($customAttributes) so you can see it
更新:
你只需要将id传给你的options
<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>
然后在您的控制器中获取它们并验证
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();
}
尝试用下面的
替换控制器函数addingItempublic 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' => $request->input('attr'), // Attributes Array
));
Session::flash('success', 'This product added to your cart successfully.');
return redirect()->back();
}
我假设您需要通过
插入多个属性@foreach($options as $optiontitle => $optioncollection)
替换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>
以下
<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>
它将创建一个属性数组来防止错误,
Trying to get property of non-object
如果您需要进一步的帮助,请发表评论。