Files
Global-Jain/app/Rules/CheckUniqueEmailHashValue.php
2025-11-05 10:37:10 +05:30

58 lines
1.0 KiB
PHP

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use App\Models\User;
class CheckUniqueEmailHashValue implements Rule
{
public $id;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($id = null)
{
$this->id = $id;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$hashed = md5($value);
$user = User::where('email_hash', $hashed);
if (isset($this->id) && !empty($this->id)) {
$user = $user->where('id', '!=', $this->id);
}
$user = $user->first();
if ($user === null) {
return true;
}
return false;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('validation.unique', ['attribute' => 'email']);
}
}