Skip to content
Advertisement

Laravel: Redirect with post route

Been trying to find a way to call to pass a multidimensional array to a Post route with no success.

The array looks like this:

    "order" => array:16 [
        "id" => "1"
        "total" => "4825"
        "neighborhood" => "Barrio Bravo"
      ]
      "products" => array:2 [
        4 => array:4 [
          "id" => "4"
          "name" => "Maestro Dobel 750ml"
          "price" => "530"
          "quantity" => "1"
        ]
        1 => array:4 [
          "id" => "1"
          "name" => "Don Julio 70 700ml"
          "price" => "650"
          "quantity" => "1"
        ]
      ]
      "grandTotal" => "1180"
      "balanceToPay" => "354"
      "cartTotal" => "826"

I don’t have any problem asserting the route in the unit test calling the route like so:

$this->post(route('order.success', $orderInfo));

But when it comes to the controller I can’t find the way to redirect to order.success with its orderInfo array.

This won’t work since redirect only works with GET:

return redirect(route('order.success', $orderInfo));

Ideas?

Advertisement

Answer

It’s not going to work with a simple redirection because you cannot choose the HTTP method. It’s always GET when you make a redirection. It works in your test because you make a POST request manually.

I can see 2 solutions:

  1. You can send data using GET method (they are added as URL parameters). If they are not confidential it can be a solution.
  2. If you don’t want to send those data in the URL, you have to save them somewhere and get them from the storage when you’re on the order.success page. You can save them, for instance, in the session storage or in the local storage of your browser.

Also, your test isn’t good if it tests a behavior that does not happen in your app (post request instead of redirection).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement