Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit the validate method in update post #26

Open
Ahmad-Fathy opened this issue Oct 16, 2018 · 0 comments
Open

Edit the validate method in update post #26

Ahmad-Fathy opened this issue Oct 16, 2018 · 0 comments

Comments

@Ahmad-Fathy
Copy link

We can edit the validate method in PostController to ignore a given ID during the unique check for slug (instead using if)

    public function update(Request $request, $id)
    {
        // Validate the data
        $post = Post::find($id);
        $this->validate($request, array(
            'title' => 'required|max:255',
            'slug' => [
                'required',
                'alpha_dash',
                'min:5',
                'max:255',
                Rule::unique('posts')->ignore($post->id)
            ],
            'body' => 'required|min:5',
        ));
        

        // Save the data to the database

        $post->title = $request->input('title');
        $post->slug = $request->input('slug');
        $post->body = $request->input('body');
        $post->save();
        // Set flash data with success message
        Session::flash('Success', 'This post was successfully updated.');
        // redirect with flash data to posts.show
        return redirect()->route('posts.show', $post->id);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant