framework icon indicating copy to clipboard operation
framework copied to clipboard

sqliteCreateFunction stopped working after laravel 12.21.*

Open ruskiyos opened this issue 1 month ago • 3 comments

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 REGEXP using sqliteCreateFunction in the register method of AppServiceProvider
  • write a query that uses a REGEX expression
  • run query against a sqlite database connection
  • see no such function error

ruskiyos avatar Oct 31 '25 02:10 ruskiyos