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

undeportedmexican's avatar

Proper response to Ajax Request

Hello All!!

So, I have a couple of modals inside my 'show' view that updates portions of the modal. I'm making it like this because, semantically it makes sense to edit both groups of information independently.

Within them modals, I'm using an Inertia Form Helper on each, to build the form and submit changes to the server. Everything is working fine, my question is, What should I return to my ajax request?

I'm currently returning an Inertia response to the 'show' view, and it's working, but I am unsure if this is the correct way, as I am having to lazy load all of the relationships that are required inside the show view. Is this the correct view?

Thanks in advance!

0 likes
2 replies
jimmitjoo's avatar

Sure, you can do it that way - but there might be more performant ways to achieve the same result.

I hope I am understanding your question properly, sorry if not :)

I'd personally make a custom response for this specific page, which just use one db query that includes everything needed for that view beforehand and try to not use lazy loading. That will make the request a little bit faster as you only need to perform one query to the database, instead of one query for every time you need to lazy load something.

It is easy to get sloppy if you are lazy loading things all over the place. To make sure I am not lazy loading stuff when I don't need to, I am using the method to prevent lazy loading when I am developing locally.

// app/Providers/AppServiceProvider.php
 
public function boot()
{
    Model::preventLazyLoading(! app()->isProduction());
}
undeportedmexican's avatar

Not sure if I follow what you're saying, let me add my code, to see if we're talking about the same thing. (Probably what I should've done in the first place LOL)

This is my Index method:

public function index() {
        $orders = Linehaul::query()
                   ->with(['stops', 'driver', 'team_driver', 'tractor', 'client'])
                   ->get();

        return Inertia::render('Orders/OrderIndex', compact('orders'));
    }

As you can see, in that method, I'm eager loading everything I need, so we're cool in that part. Inisde that view, I have a couple of modals that update different things, both modals go to the same 'update' method (I'm using the validator to filter the fields that need updating), this is the update model:

public function update(Request $request, Linehaul $order) {

        $validated_fields = $request->validate([
            'driver_id'      => 'integer',
            'client_id'      => 'integer',
            'team_driver_id' => 'integer|nullable|exclude_if:team_driver_id,null',
            'tractor_id'     => 'integer',
            'load_type'      => 'string',
            'mode'           => 'string',
            'category'       => 'string',
            'rate'           => 'numeric',
            'trailers'       => 'array'
        ]);

        $trailers = Arr::pluck(Arr::pull($validated_fields, 'trailers'), 'id');

        $order->update($validated_fields);

        $order->trailers()->sync($trailers);

        $order->load(['client', 'driver', 'trailers', 'team_driver', 'tractor', 'stops']);

        return Inertia::render('Orders/OrderShow', compact('order'));
    }

When I'm returning to my Index view, which had already eager loaded all the required relationships, I need to re-load (lazily this time) all the relationships, otherwise my view breaks.

Am I following proper convention here?

Please or to participate in this conversation.