将所选数量和价格存储在数组中

时间:2018-04-06 22:05:31

标签: php laravel

我有一个会议详细信息页面,其中包含一个表单,因此用户可以为每种tiket类型选择他想要的数量,形式如下:

<form method="post" action="{{route('congresses.registration', ['id' => $congress->id, 'slug' => $congress->slug])}}">
    <ul>
            {{ csrf_field() }}
            @foreach($tickets as $ticket)
                <li>
                    <div>
                        <span>{{$type->name}}</span>
                    </div>
                    <select name="types[{{ $type->name }}]">
                        <option selected></option>
                        @for ($i = $type->minPerUser; $i <= $type-> maxPeruser; $i++)
                            <option value="{{ $i }}">{{ $i }}</option>
                        @endfor
                    </select>
                    <span>X {{$type->price}}</span>
                </li>
            @endforeach
    </ul>
    <div class="card-footer">
        <input type="submit" value="Next" />
    </div>
</form>

当&#34;下一个&#34;单击按钮我想在RegistrationController中存储用户选择的数量以及storeQuantity()方法中每种类型的价格,以便可以将此信息发送到注册页面。并且在此方法中将用户重定向到注册页面。

我有下面的代码但是我没有成功将所选数量和价格存储在数组中,因此可以将具有必要信息的数组返回到注册视图。你知道怎么做吗?

public function storeQuantity(Request $request){
        $this->validate($request, [
        ]);

        //dd($request->all());

        $typeQuantities = $request->get('types');
        foreach($typeQuantities as $typeName => $quantity){
            $type = TicketType::where('name', $typeName)->firstOrFail();
            $price = $type->price;
            //dd($price);    shows the price, example "10"
        }

        return view('congresses.registration')->with('selectedtypes', $selectedtypes);
    }

&#34; dd($ request-&gt; all());&#34;所示:

array:2 [▼
  "_token" => ""
  "types" => array:2 [▼
    "center bench" => "2"
    "lateral bench" => "1"
  ]
]

1 个答案:

答案 0 :(得分:1)

在foreach中尝试这个:

...
$price = $type->price;
$selectedtypes[$type->name]['qty'] = $quantity;
$selectedtypes[$type->name]['price'] = $price;