cviebrock / eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel
MIT License
3.91k stars 461 forks source link

Redirecting to model/{id} after resource update method, resulting in 404 not found. #567

Closed dhofstedt closed 3 years ago

dhofstedt commented 3 years ago

After submitting a resource update form on a sluggable model, a 404 not found error occurs due to a redirect to the specific resource being updated. Rather than redirecting to posts.index as specified in the update() method, it is redirecting to (or getting stuck at?) posts/{id}.

There are no issues with posts/{slug} or posts/{slug}/edit, only this issue on update(). There is normal update behavior before making the model sluggable and again after reverting to its pre-sluggable state.

The route:list looked fine, but I ran route:clear anyways and still the same thing happens.

kerrinhardy commented 3 years ago

I've just started using this package on a new product (Laravel 8) and am seeing the same thing.

On update, I have the update method to return redirect($idea->path())->with('success-message', 'Idea updated');

The path() method is:

public function path()
    {
        return url('ideas', [$this->slug]);
    }

But, like you, it is landing at 'ideas/{id}' and resulting in a 404.

kerrinhardy commented 3 years ago

Update: I actually had the id being referenced in the form action. Changed it to

<form method="POST" action="{{ route('ideas.update', $idea) }}">

and it is working properly now.

cviebrock commented 3 years ago

@dhofstedt Make sure you have Route Model Binding set up correctly as per the docs. I believe you want to use /post/{post} as the route, not /post/{id}.

@kerrinhardy That works as well.