How to use unique validation in laravel?

I have a comment table:

enter image description here

The My Comment controller, where it takes values ​​from the form and validates it and stores it in the 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 verify that only a unique email identifier can comment on each product_id, if the user has already commented on product_id 1, then the same user cannot comment on this identifier again.

I tried

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

But since you can see that the same email id data was entered for the same product_id.

The user can comment on the product_id page number, but cannot comment again once he has commented on this identifier.

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

enter image description here

If another product_id can then enter an email address.

Thanks.

+5
source share
5 answers

Write it in the console

 php artisan make:provider ValidationServiceProvider 

Then replace 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. - . Make sure Model App\Comment replaced by the namespace of the modal used for your comment system.

Now add it to the providers in config/app.php , for example

 App\Providers\ValidationServiceProvider::class, 

Now you can check the existence of the string as follows:

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

You can also add a special message like this

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

Note that I have not tested the code, but this should work if all the data is transferred correctly and all namespaces are used correctly.

+2
source

This does not work as a rule unique . For your task, you need to create your own validation rule and check manually if the user has already commented on this product with this letter.

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

0
source

Try unique_with custom validation. Perhaps this package will be completely filled with your requirement. You can check and get an example here .

0
source

It is not possible to use multiple columns for a unique rule. You must manually verify this by clicking the query.

0
source

You can use the unique validation rule. Link

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 helps you

0
source

Source: https://habr.com/ru/post/1261026/


All Articles