即使未选中,Laravel下拉列表也会获得所有选项

时间:2018-01-23 01:56:41

标签: php laravel laravel-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];
});

这是我的购物车方法,事情发生了,一切都将添加到购物车而不是仅选择。

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\"}},....

这是我在函数循环中获得labelprice的地方。

如果我使用循环,例如:

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

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

  

尝试获取非对象的属性

在这一行:

  'attr' => [
    'label' => $subs->title,
    'price' => $subs->price,

PS: 如果我使用$customAttributes = $request->attr;和我自己的循环(在顶部),我将只选择id选项,这对我没有好处。

3 个答案:

答案 0 :(得分:4)

因为您调用了产品的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>

并在您的控制器中,获取它们并验证

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();
}

答案 1 :(得分:1)

您是否不能从订单或请求中获得选项而不是从Prodect获得选项?!
因为产品显然应该有所有选择 ...........

- 编辑:1

像这样改变你的循环

foreach($request->attr as $subs)

并将您的selct标记更改为类似

<select multiple>

-Edit2

编辑完问题后,我认为您有两种选择:

  1. 使用每个$ subs中的id从DB
  2. 再次查询子选项

    OR

    1. 在前端html选项标记中序列化标题和价格,以便最终得到如下内容:

      <option   value="{{$suboption->title.','.$suboption->price}}" .....> .....</option>
      
    2. 然后在你的循环中这样做:

      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我发现了另一个问题:

      你应该使用name =&#34; {{$ optiontitle}} []&#34;因为他们所有的canot都具有相同的名称,所以你必须使select标签的name属性动态化

答案 2 :(得分:0)

尝试用下面的

替换控制器功能 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 =&gt; $ optioncollection)

更换刀片代码,

<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>

它将创建一个可以防止错误的属性数组

  

尝试获取非对象的属性

如果您需要进一步的帮助,请发表评论。