laravel - 将数据从第一页传递到第三页

时间:2021-07-07 19:33:47

标签: php laravel

我有一个预订系统,我将其分为三个步骤。首先,用户可以输入包裹重量、邮政编码等,然后生成快递费报价,用于下一步。

其次,它显示了一个费率列表供快递员选择,用户必须选择一个快递员并进入下一页,即预订订单页面。

第三,这个页面包含一个表单,一些字段如包裹重量、邮政编码等将按照您的预期生成。所以基本上,我必须将这些数据从第一个视图一直传递到第三个视图,以及从第 2 页到第 3 页选择的报价以完成预订。

总结:包裹页面(输入包裹信息)->报价页面(选择快递)->预订订单页面(提交订单)

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>

我能够以这种方式检索数据,尽管我觉得这样做可能过于复杂。而且,隐藏的输入也很容易受到攻击。是否有另一种方法可以将数据从开始(第一页)一直传递到结束(第三页)而不按我的方式进行?

如何在会话中保存数据(从第一个检索),以便在最后一页检索它?

3 个答案:

答案 0 :(得分:1)

->with('rates', $rates)session()->push('rates', $rates) 对您在此处需要执行的操作无效。由于 $rates 是 Eloquent Collection,因此推送到 session() 会消除所有搜索关系的能力,例如 $rate->relationship->property。要处理此问题,请在您的 GET 处理程序中传递 ID 和查询:

在您的 POST 方法的处理程序中:

public function quotation(Request $request) {
  // ...

  $rateIds = Rate::where('weight', $request->parcel_weight)
  ->where('location_id', $location_id)
  ->pluck('id');

  return redirect()->route('quotation.show')->with([
    'rate_ids' => $rateIds,
    'weight' => $request->input('parcel_weight'),
    'postcode_delivery' => $request->input('postcode_delivery')
  ]);
}

这将从您的数据库返回 rates.id 列的集合。然后您可以在 Storage 中传递它并在您的 GET 处理程序中查询:

public function showQuotation(){
  if (!session()->has('rate_ids')) {
    return abort(403);
  }
  
  $rates = Rate::with('courier.image')
  ->whereIn('id', session()->get('rate_ids'))
  ->get();
  
  return view('orders.quotation')->with([
    'rates' => $rates,
    'weight' => session()->get('weight')
    'postcode_delivery' => session()->get('postcode_delivery')
  ]);
}

现在在您的 orders/quotation.blade.php 中,您可以执行 @foreach ($rates as $rate),而 $rate->relationship->property 会正常工作。

答案 1 :(得分:0)

因此,您将需要的任何内容推送到报价会话中,并在下一个方法中使用它。

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::push('rates', $rates)
    Session::push('weight', $request->parcel_weight)

    return redirect()->route('quotation.show');
   
}

//Show quotation
public function showQuotation(){

    $rates = Session::get('rates')
    $weight= Session::get('weight')

   //check if form is submitted
    if($rates){
       
        return view('orders.quotation');
    }

        abort(403);
}

//Show booking page
public function createOrder(Request $request){
    // access whatever you need from session
    $rates = Session::get('rates')
    $weight = Session::get('weight ')

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

答案 2 :(得分:-1)

我有两个解决方案。

  1. 在完成第1步后在表中创建一个临时行。然后将id从1传递到2,从2传递到3。在第3步完成后。然后让你的 is_completed 列 1。

  2. 使用会话。在第 1 步之后生成一个 uuid。将表单数据保存到会话。然后在 2 和 3 中使用会话。

相关问题