Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AungHtetPaing__'s avatar

How to redirect vue component with inertia

Hello, i am doing shopping cart checkout and after checkout i want to redirect to thankyou or order summary component with order data with eager load products data. I can't use Inertia::render it go back to axios.post then method. So i just return $order and use inertia manual visits but i getting Call to a member function load() on array error in checkoutController when loading products data.

So how to fix it and can't i just render vue component from controller?.

checkoutController.php

public function thankyou(Request $request)
    {
        $order = $request->get('order');
        
        return Inertia::render('thankyou', ['order' => $order->load('books')]);
    }

Checkout.vue

axios
          .post("/checkout", this.customer)
          .then((response) => {
            this.paymentProcessing = false;
            console.log(response);
            this.$inertia.get(`/thankyou`, { order: response.data });
          })
          .catch((error) => {
            this.paymentProcessing = false;
            console.error(error);
          });
0 likes
6 replies
bobbybouwmann's avatar

The problem is not in Inertia, but rather in your controller

Let's say that the order ID you post is 2. THen $order contains the value 2. You first have to retrieve the order from the database, before you can call load on it

$order = Order::findOrFail($request->get('order'));
$order->load('books');

If you need to retrieve the order anyway, you can also use with here

$order = Order::with('books')->findOrFail($request->get('order'));
1 like
AungHtetPaing__'s avatar

@bobbybouwmann I got 404 not found. I am confusing it should work. Here is my response.data i pass to controller.

data:
address: "padauk"
city: "yangon"
created_at: "2021-10-24T05:15:41.000000Z"
id: 19
state: "yangon"
total: 5729
transaction_id: "ch_3Jnz3XK3AEGA3BHp1EIHXeJU"
updated_at: "2021-10-24T05:15:41.000000Z"
user_id: 1
zip_code: "12344"
AungHtetPaing__'s avatar

When i check $request->get('order')[id], i get number. So I changed like this Order::with('books')->findOrFail($request->get('order')['id']) and I get another error ReferenceError: order is not defined

bobbybouwmann's avatar
Level 88

I think you're mixing a lot of stuff here. Instead of passing the full order { order: response.data }) to the GET request of the thankyou page, it's probably better to simply pass along the order id. You can even decide to just put the ID in the URL. Right now it will be appended as thankyou?order=???.

Anyway, try this instead

this.$inertia.get(`/thankyou`, { order: response.data.id }); // notice the .id added here

// Controller
$order = Order::with('books')->findOrFail($request->get('order'));

If that doesn't work you can always put the ID in the URL

this.$inertia.get('/thankyou/' + response.data.id);

// Controller
public function thankyou(Order $order)
{
	$order->load('books');

	return Inertia::render('thankyou', ['order' => $order->load('books')]);
}

Note that we use route model binding in the above example. It will retrieve the order model automatically for you based on the ID.

Documentation: https://laravel.com/docs/8.x/routing#route-model-binding

1 like

Please or to participate in this conversation.