webfuelcode
6 days ago

Updating the post redirects with id no the slug

Posted 6 days ago by webfuelcode

What is wrong here that I see 404 error. I am using slug from `cviebrock/eloquent-sluggable.

Updating redirects with id not the slug...

public function update(Request $request, Post $post)
    {
        $this->validate($request, [
            'title' => 'required',
            'category_id' => 'required',
            'description' => 'required'
        ]);

        $post->update();
        return redirect()->route('post.show', $post->slug)->withMessage('Updated successfully!');
    }

Where the store method works fine and redirects to a single page with slug.

public function store(Request $request)
    {
        $validatedData = $this->validate($request, [
            'title'         => 'required|min:3|max:255',
            //'slug'          => 'required|min:3|max:255|unique:posts',
            //'image'         => 'sometimes|image',
            'category_id'   => 'required|numeric',
            'description'   => 'required|min:10'
        ]);
    
        $validatedData['user_id'] = Auth::id();
        //$validatedData['slug'] = SlugService::createSlug(Post::class, 'slug', $request->title);//Str::slug($validatedData['slug'], '-');
    
        $post = Post::create($validatedData);

        return redirect()->route('post.show', ['post' => $post->slug])->withMessage('Post created successfuly!');
    }

Please sign in or create an account to participate in this conversation.