diff --git a/src/Validation/DatabasePresenceVerifier.php b/src/Validation/DatabasePresenceVerifier.php index fdd783ab5..498cecd70 100644 --- a/src/Validation/DatabasePresenceVerifier.php +++ b/src/Validation/DatabasePresenceVerifier.php @@ -17,7 +17,7 @@ class DatabasePresenceVerifier extends \Illuminate\Validation\DatabasePresenceVe #[Override] public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = []) { - $query = $this->table($collection)->where($column, new Regex('^' . preg_quote($value) . '$', '/i')); + $query = $this->table($collection)->where($column, new Regex('^' . preg_quote($value) . '$', 'i')); if ($excludeId !== null && $excludeId !== 'NULL') { $query->where($idColumn ?: 'id', '<>', $excludeId); diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 9d2089af5..24cf281e5 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -5,7 +5,9 @@ namespace MongoDB\Laravel\Tests; use Illuminate\Support\Facades\Validator; +use MongoDB\BSON\Regex; use MongoDB\Laravel\Tests\Models\User; +use MongoDB\Laravel\Validation\DatabasePresenceVerifier; class ValidationTest extends TestCase { @@ -132,4 +134,44 @@ public function testExists(): void ); $this->assertFalse($validator->fails()); } + + public function testGetCountBuildsRegexWithValidFlags(): void + { + $query = new class { + public $value; + + public function useWritePdo() + { + return $this; + } + + public function where($column, $value) + { + $this->value = $value; + + return $this; + } + + public function count() + { + return 0; + } + }; + + $verifier = new class ($query) extends DatabasePresenceVerifier { + public function __construct(private $query) + { + } + + protected function table($table) + { + return $this->query; + } + }; + + $verifier->getCount('users', 'name', 'John Doe'); + + $this->assertInstanceOf(Regex::class, $query->value); + $this->assertSame('i', $query->value->getFlags()); + } }