【问题标题】:laravel - passing data from first page to third pagelaravel - 将数据从第一页传递到第三页
【发布时间】: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 请求,将输入的包裹重量、邮政编码等推送到会话,然后返回包含第二个表单的下一页。然后做同样的事情
  • 您介意演示或发布答案吗?

标签: php laravel


【解决方案1】:

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

在您的 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 列的集合。然后,您可以在存储中传递它并在您的 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-&gt;relationship-&gt;property 可以正常工作。

【讨论】:

  • 那么明白了。 session()-&gt;get('rate_ids') 给了我正确的值。
  • 集合本质上是具有额外功能的数组 :) Model::whereIn('id', [1, 2, 3])Model::whereIn('id', collect([1, 2, 3])) 都将返回 ID 1、2 和 3 的 Model 记录。
  • 这是正确的;现在,当您执行Rate::whereIn('id', ...) 时,它将返回rates 行,其中id 为1、62、121、181、241、301、361、421、481 和541。
  • 是的,我包含了with('courier.image') 以避免在执行@foreach ($rates as $rate) 后跟$rate-&gt;courier-&gt;image-&gt;whatever 时产生20 个额外的查询。
  • 之前有人告诉我 ? 一如既往地乐于助人。干杯!
【解决方案2】:

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

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

【讨论】:

  • 但是我已经推送到与with('weight', session('weight),etc 的会话在我的刀片中,我使用foreach($rates as $rate)... 检索它
  • 那么,您还缺少什么? createOrder() 方法还需要什么?
  • 如果您使用会话,然后在刀片中填充数据,我假设您必须以不同的方式进行操作。我还需要链接with() 吗?
  • 没有。您可以将某些内容推送到会话,打开 5 个不同的页面,相同的内容仍然存在。
  • 但是根据你的回答,如果你已经推送到会话,为什么你还需要-&gt;with('rates',$rates)
【解决方案3】:

我有 2 个解决方案。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多