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 { public function boot() { Validator::extend('unique_email_comment', function($attribute, $value, $parameters, $validator) { return !Comment::where('email', $value)->where('product_id', $parameters[0])->exists(); }); } 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.
source share