Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Validation/DatabasePresenceVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}