6

I have comments table:

enter image description here

My Comment controller where its taking values from form and validating it and storing it in database.

public function store(Request $request, $product_id)
    {
        $this->validate($request, array(
            'name' => 'required',
            'email'=> 'required|email|unique:comments,product_id',
            'comment' => 'required',
            'rating' => 'required|integer'
            ));
        $product = Product::find($product_id);
        $comment = new Comment();
        $comment->name = $request->name;
        $comment->email = $request->email;
        $comment->comment = $request->comment;
        $comment->rating = $request->rating;
        $comment->product()->associate($product);

        $comment->save();
        Session::flash('success','Comment was added');
        return redirect()->route('product.show',[$product->id]);
    }

I am trying to validate such that only unique email id is allowed to comment on every product_id that is if user has already commented on product_id 1 then the same user is not allowed to comment again on that id.

I tried

'email'=> 'required|email|unique:comments,product_id'

But as u can see about same email id data was entered for same product_id.

A user can comment on n number of product_id page but cannot comment again once he has commented on that id.

Is there a way to validate if email data has been entered in database for particular product_id then the same email is not allowed to submit the form.

enter image description here

If different product_id then email can enter.

Thanks.

0

5 Answers 5

2

Write this in your console

php artisan make:provider ValidationServiceProvider

Then replace your App\Providers\ValidationServiceProvider with

namespace App\Providers;

use Validator;
use App\Comment;
use Illuminate\Support\ServiceProvider;

class ValidationServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot() {
    Validator::extend('unique_email_comment', function($attribute, $value, $parameters, $validator) {
      return !Comment::where('email', $value)->where('product_id', $parameters[0])->exists();
    });
  }

  /**
   * Register the service provider.
   *
   * @return void
   */
  public function register() {
    //
  }
}

Note :- Please ensure the Model App\Comment is replaced with the namespace of the modal you use for your comments system.

Now add it to providers in config/app.php like

App\Providers\ValidationServiceProvider::class,

Now, you can validate the existence of the row by:

'email' => 'unique_email_comment:' . $productId;

// or
// 'email' => 'unique_email_comment:' . request()->get('product_id');

You can also add a custom message like this

return [
  'email.unique_email_comment' => 'A comment has already been made for this product with the email provided';
]

Please note that I haven't tested the code out but this should work if all the data is passed correctly and all namespaces are correctly used.

3
  • @Phoenix Glad I could be of help :) Dec 9, 2016 at 11:58
  • where do I add custom message
    – Phoenix
    Dec 9, 2016 at 12:03
  • Simply make a custom $messages = [....] and add it as a third parameter to Validator::make() like Validator::make($inputs, $rules, $messages) Dec 9, 2016 at 12:04
0

This is not how unique rule works. For your task you need to create custom validation rule and check manually if user with this email had already commented this product.

https://laravel.com/docs/5.3/validation#custom-validation-rules

1
  • can you please create a sample rule according to problem above as I can't figure out after reading documentation as I am not so experienced with laravel. Thanks.
    – Phoenix
    Dec 9, 2016 at 7:10
0

Have you try unique_with custom validation. May be this package with full fill your requirement. You can check and get example here.

0

There is no way that you can use multiple columns for the unique rule. You have to manually check that by firing a query.

0

You can use the validation rule unique. Reference

use Illuminate\Validation\Rule;

$this->validate($request, array(
     'name' => 'required',
     'email'=> ['required', 'email', Rule::unique('comments', 'email')->where(function($query) {
                    $query->where('product_id', $product_id);
               })],
     ...
));

Hope this will help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.