anthonyedmonds / laravel-mysqlite
Generate MySQL and Sqlite3 expressions on the fly based on the active database
Installs: 1 889
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.3
- illuminate/support: ^11
Requires (Dev)
- laravel/pint: ^1
- orchestra/testbench: ^9
- phpstan/phpstan: ^1
- phpunit/phpunit: ^11
README
Generate MySQL and SQLite3 expressions on the fly based on the active database.
Designed primarily for situations where an SQLite3 database is used for testing, but MySQL is used for production.
Calling the MySqlite::
helper will provide the correct syntax for the default database connection.
Installation
- Install via Composer:
composer require anthonyedmonds/laravel-mysqlite
- Use in your code:
use AnthonyEdmonds\LaravelMySqlite\MySqlite
Usage
Laravel MySqlite provides syntax for the following MySQL/SQLite conversions:
The helper returns an Expression
with custom __toString
behaviour, so you may use it directly inside queries:
DB::table('users') ->select([ MySqlite::concat(['users.first_name', '" "', 'users.last_name'], 'name'), MySqlite::dateFormat('users.created_at', '%d/%m/%Y', 'formatted_date'), MySqlite::day('AVG(users.updated_at)', 'aggregated_day'), MySqlite::trim('", "', 'users.aliases_list', 'formatted_aliases', MySqlite::TRIM_TRAILING), ]) ->groupBy([ MySqlite::year('users.updated_at') ]) ->get();
Columns
The column/columns value of a method accepts a string/array of:
- A column name
'users.first_name'
- An aggregate method
'COUNT(users.id)'
- A string literal in quotation marks
'"Literally this"'
- Any other valid SQL expression
Aliasing / As
The optional $as
parameter adds a column alias when set:
MySqlite::hour('users.created_at', 'potato') === HOUR(users.created_at) AS potato
Roadmap
There are more syntax differences between MySQL and SQLite that can be compensated for with minor syntax changes, which will be added as they are encountered.
If you require a particular method conversion, raise an issue.