table = $table; $this->field = $field; $this->query = DB::table($this->table); } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool * @throws \Throwable */ public function passes($attribute, $value) { throw_if(is_array($value), 'UniqueRule won\'t work in array values like ' . $attribute); return empty($value) ? true : !$this->query->where($this->field, $value)->exists(); } public function withoutTrashed($deletedAtColumn = 'deleted_at'): UniqueRule { $this->query->whereNull($deletedAtColumn); return $this; } public function where($column, $value): UniqueRule { $this->query->where($column, $value); return $this; } public function ignore($id): UniqueRule { if (empty($id)) { return $this; } $this->query->whereNot('id', $id); return $this; } /** * Get the validation error message. * * @return string */ public function message() { return 'select value already exists.'; } }