【发布时间】:2021-07-07 19:33:47
【问题描述】:
我有一个预订系统,我分为三个步骤。首先,用户可以输入包裹重量,邮政编码等,然后生成一个快递费率报价,进入下一步。
其次,它显示了一个供快递员选择的费率列表,用户必须选择一个快递员并进入下一个页面,即预订订单页面。
第三,这个页面包含一个表单,一些字段,如包裹重量,邮政编码等,将按您的预期生成。所以基本上,我必须将这些数据从第一个视图一直传递到第三个视图,以及从第二页到第三页选择的报价,以完成预订。
总结:包裹页面(输入包裹信息)->报价页面(选择快递)->预订订单页面(提交订单)
web.php:
//Create new order
Route::get('/dashboard/orders','Dashboard\OrderController@index')->name('order.index');
//Generate quotation
Route::post('/dashboard/orders/quotation','Dashboard\OrderController@quotation')->name('order.quotation');
//Quotation page
Route::get('/dashboard/orders/quotation','Dashboard\OrderController@showQuotation')->name('quotation.show');
//Booking order page
Route::post('/dashboard/orders/booking','Dashboard\OrderController@createOrder')->name('create.quotation');
//Show booking order page
Route::get('dashboard/orders/booking','Dashboard\OrderController@showBooking')->name('show.booking');
OrderController.php:
//Create quotation
public function quotation(Request $request){
$validated = $request->validate([
'parcel_weight' => 'required',
'parcel_size' => 'required',
'postcode_pickup' => 'required|postal_code:MY|exists:postcodes,postcode',
'postcode_delivery' => 'required|postal_code:MY|exists:postcodes,postcode'
]);
//logic to compute the quotation rate for each courier based on the inputs
//default - postcode belongs to Penisular Malaysia
$location_id = 1;
if((88000<= $request->postcode_delivery) && ($request->postcode_delivery <= 91309) ){
//check postcode belongs Sabah
$location_id = 2;
}
if((930000<= $request->postcode_delivery) && ($request->postcode_delivery <= 98859) ){
//check postcode belongs to Sarawak
$location_id = 2;
}
$rates=Rate::where('weight',$request->parcel_weight)->where('location_id',$location_id)->get();
//session()->put('submitted', true);
return redirect()->route('quotation.show')
->with('rates',$rates)
->with('weight',$request->parcel_weight)
->with('postcode_delivery',$request->postcode_delivery);
}
//Show quotation
public function showQuotation(){
//check if form is submitted
if(session()->has('rates')){
return view('orders.quotation')->with('rates',session('rates'))
->with('weight',session('weight'))
->with('postcode_delivery',session('postcode_delivery'));
}
abort(403);
}
//Show booking page
public function createOrder(Request $request){
//// Should be able to retrieve request from courier rate chosen, the weight of parcel, postcode
dd($request->all());
}
quotation.blade.php:
<div class="row">
<div class="offset-1 col-11">
<h4 style="font-weight: 700">Regular Order</h4>
<p>Please select one courier:</p>
<form method="POST" action="{{route('create.quotation')}}">
@csrf
<div class="row">
@foreach($rates as $rate)
<div class="col-3 col-md-2">
<!-- ISSUE HERE------>
<input type="hidden" value="{{$weight}}">
<img style="height: 60px; width:80px;" src="{{asset($rate->courier->image->path .''.$rate->courier->image->filename)}}" alt="{{$rate->courier->image->filename}}">
<div>
<input type="radio" class="mt-1" name="courier" value="{{$rate->cost}}">
<label for="{{$rate->courier->name}}">RM{{$rate->cost}}</label>
</div>
</div>
@endforeach
</div>
<div class="offset-md-3 mt-4">
<button type="submit" id="submit" class="btn" style="border-radius: 20px; background:#efcc37;">Book Delivery</button>
</div>
</form>
</div>
</div>
我能够以这种方式检索数据,尽管我觉得这样做可能过于复杂。而且,隐藏的输入也很容易受到攻击。是否有另一种方法可以不按我的方式将数据从开始(第一页)一直传递到结束(第三页)?
如何在会话中保存数据(从第一个检索),以便在最后一页中检索它?
【问题讨论】:
-
你会考虑对数据使用会话吗?
-
具体针对哪些数据?
-
例如,您发送一个 POST 请求,将输入的包裹重量、邮政编码等推送到会话,然后返回包含第二个表单的下一页。然后做同样的事情
-
您介意演示或发布答案吗?