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')]);
}
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
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
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.