Home > Mobile >  Laravel 9 - Submitting a form to a route leads to /null and doesn't do anything
Laravel 9 - Submitting a form to a route leads to /null and doesn't do anything

Time:12-13

I'm using soft deletes and trying to make a function to restore the deleted row. Really everything to do with submitting the form doesn't work, even deleting...

My tags are within a forelse loop, maybe that's the cause??

Route is (this is above my resource route):

Route::post('/post/restore/{id}', [PostController::class, 'restore'])
    ->name('post.restore');

Controller is:

public function restore($id)
{
    dd($id);
}

Form/view is:

<form action="{{route('post.restore', $post->id)}}" method="POST">
@csrf

<button type="submit"  data-confirm="Would you like to restore?">Restore</button>
</form>

After submitting, it just takes me to:

domainURL/post/null

and gives a 404 error

Any advice?? I also tried it without the {id} at the end of the route, same results

CodePudding user response:

I think you are using the wrong syntax for route function, instead of this:

route('post.restore', $post->id)

you should use it like this:

route('post.restore', ['id' => $post->id])

you can read more here as well.

CodePudding user response:

I figured it out... my "would you like to restore?" javascript function was breaking the form. I was using javascript to popup a confirmation message before submitting and during that process it lost the ID

I got rid of that and it works fine

Thank you!

  • Related