inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.09k stars 235 forks source link

Route model binding->missing method not working as expected #495

Closed bhaviljain68 closed 1 year ago

bhaviljain68 commented 1 year ago

Hi,

So I came across this issue and posted it on laracasts forums.

I'm creating an app with Inertia and vuejs.

So I have a resource controller and the route for it is as following

Route::apiResource('course-info-sections', CourseInfoSectionController::class)
        ->parameters([
            "course-info-sections" => "section"
        ])
        ->missing(function ()
        {
            return redirect()->back()->withErrors([
                "Section Not Found"
            ]);
        });

in Vue (manually supplying an invalid ID)

const deleteSection = function () {
  router.delete(route("course-info-sections.destroy", "20"), {
    onSuccess: () => (deleteModalOpen.value = false),
    onError: (e) => toast.error(e[0]),
  });
};

the route for delete and update have a URL parameter section

the route works perfectly when supplied with the proper ID, but when I manually supply a wrong ID (one that doesn't exist in the DB). it would show me a 404 page (which is the expected behaviour), I searched the docs and found the "missing" method and applied it as above, but now instead of redirecting it back I get an error

The course-info-sections method is not supported for route PATCH. Supported methods: GET, HEAD, POST. 
I tried to dd("Not Found") in the closure of the missing method, and that works.

Long story short, the workaround found by Sinnbeck was to change the order of middleware in Kernel.php, I did that and the problem is solved

here is his reply

Got it working :D

It depends on the order of your middleware in your Kernel.php. The inertia middleware MUST come before the SubstituteBindings

This doc says to put it last, but that means it wont work with this. https://inertiajs.com/server-side-setup#middleware

I cannot guarantee that it does not give other issues

he mentions that he's unsure if changing the order could break other functionalities, as inertia docs asks to put the inertia middleware at the very end.

here is a link to the entire thread

Unsure if this is a bug or the docs need to be updated (i.e. inertia middleware doesn't need to be at the very end).

jessarcher commented 1 year ago

Hey there,

We're closing this issue because it's inactive, already solved, old or not relevant anymore. Feel to open up a new issue if you're still experiencing this problem.