Search code examples
laravelcontroller

Laravel:: How to attach a Request instance to redirect?


I have a controller function that accepts Request instant from form through a get method, I want to redirect to this route from the controller and attach a Request to it.

Route

Route::get('/{order_id}/return-calc', 'returnCalc')->name('return-calc');

Controller

public function return(Request $request, $order_id)
{
    $order = Order::with([
        'products' => fn ($q) => $q->with('thumbnail')
    ])->findOrFail($order_id);

    if ($request->method() == 'GET') {
        return view('front.orders.return_products', compact('order'));
    } elseif ($request->method() == 'DELETE') {
        $request = new Request([
            'products_ids' => $order->products->pluck('id')->toArray(),
            'quantities' => $order->products->pluck('pivot.quantity')->toArray(),
        ]);

        return redirect()->route('front.orders.return-calc', [$order_id, $request]);
    }
}

Solution

  • you can use the with to send data:

    return redirect()->route('front.orders.return-calc')->with(['order_id' => $order_id, 'request' => $request]);
    

    and then for reading, you can use session:

    $order_id = Session::get('order_id');
    $request  = Session::get('request');
    

    or

    $order_id = session()->get('order_id');
    $request  = session()->get('request');