framework
framework copied to clipboard
sqliteCreateFunction stopped working after laravel 12.21.*
Laravel Version
12.21.0
PHP Version
8.4.14
Database Driver & Version
SQLite
Description
Since sqlite does not support the REGEXP function, the pdo allows you to create a custom function to handle the unsupported method.
// AppServiceProvider.php
public function register(): void
{
if (DB::Connection() instanceof SQLiteConnection) {
DB::connection()->getPdo()->sqliteCreateFunction('REGEXP', function ($pattern, $value) {
mb_regex_encoding('UTF-8');
$value = str_replace('[[:space:]]', '\s', $value);
// workaround for escaping plus sign in regex pattern
$pattern = str_replace('\\\\+', '\+', $pattern);
return (false !== mb_ereg($pattern, $value)) ? 1 : 0;
});
}
}
This is a sample query that used to work.
$numericBrandCount = User::where('brand_id', 'REGEXP', '[0-9]+')->count();
After upgrading to laravel 12.22.* (or any version after that), the query breaks with the following error:
SQLSTATE[HY000]: General error: 1 no such function: REGEXP (Connection: sqlite, SQL: select count(*) as aggregate from "user" where "brand_id" REGEXP [0-9]+ and "user"."deleted_at" is null
If you roll back to laravel version 12.21.0, the query runs fine.
I have been unable to figure out what changed and therefore I am unable to create a Pull Request for it.
Steps To Reproduce
- Install laravel version 12.22.0 or higher
- define a sqlite custom function for
REGEXPusingsqliteCreateFunctionin theregistermethod ofAppServiceProvider - write a query that uses a
REGEXexpression - run query against a
sqlitedatabase connection - see
no such functionerror