table = $table; $this->column = $column; } /** * Set the ID of the record to be ignored. * * @param integer $id * @return $this */ public function ignore($id) { $this->ignoreId = $id; return $this; } /** * Determine if the validation rule passes. * * @param mixed $attribute Not used. * @param mixed $value The value to compare. * @return boolean */ public function passes(mixed $attribute, mixed $value) { $columnName = ($this->column ?? $attribute); $similarValue = preg_replace('/[^A-Za-z]/', '', strtolower($value)); $query = DB::table($this->table) ->whereRaw('LOWER(REGEXP_REPLACE(' . $columnName . ', \'[^A-Za-z]\', \'\')) = ?', [$similarValue]); if ($this->ignoreId !== null) { $query->where('id', '<>', $this->ignoreId); } $result = $query->first(); return $result === null; } /** * Get the validation error message. * * @return string */ public function message() { return 'The :attribute is similar to an existing value in the database.'; } }