Download the PHP package philiprehberger/laravel-db-expressions without Composer
On this page you can find all versions of the php package philiprehberger/laravel-db-expressions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download philiprehberger/laravel-db-expressions
More information about philiprehberger/laravel-db-expressions
Files in philiprehberger/laravel-db-expressions
Package laravel-db-expressions
Short Description Database-agnostic SQL expression helper for date truncation, extraction, and differences across SQLite and MySQL
License MIT
Homepage https://github.com/philiprehberger/laravel-db-expressions
Informations about the package laravel-db-expressions
Laravel DB Expressions
Database-agnostic SQL expression helper for date truncation, extraction, and differences across SQLite and MySQL.
Requirements
- PHP 8.2+
- Laravel 11 or 12
Installation
The service provider and facade are registered automatically via Laravel's package discovery.
Usage
All methods are static and return plain SQL strings suitable for use in Eloquent's selectRaw, groupByRaw, orderByRaw, and whereRaw calls.
Date Truncation (for GROUP BY buckets)
Group records into time buckets using the dateTrunc* methods or the general-purpose dateFormat dispatcher.
Real Eloquent query example:
Date Part Extraction (integer values)
Extract individual date components as integers.
Real Eloquent query example:
Date Differences
Calculate the difference between two datetime columns.
Real Eloquent query example:
Date Arithmetic
Add or subtract days from a datetime column.
Real Eloquent query example:
Facade
You can also use the DbExpressions facade:
Driver Detection
Multi-Driver Support
| Method | SQLite | MySQL / MariaDB |
|---|---|---|
dateTruncHour |
strftime('%Y-%m-%d %H:00:00', col) |
DATE_FORMAT(col, '%Y-%m-%d %H:00:00') |
dateTruncDay |
strftime('%Y-%m-%d', col) |
DATE_FORMAT(col, '%Y-%m-%d') |
dateTruncWeek |
strftime('%Y-%W', col) |
DATE_FORMAT(col, '%Y-%u') |
dateTruncMonth |
strftime('%Y-%m', col) |
DATE_FORMAT(col, '%Y-%m') |
dateTruncYear |
strftime('%Y', col) |
DATE_FORMAT(col, '%Y') |
extractHour |
CAST(strftime('%H', col) AS INTEGER) |
HOUR(col) |
extractDay |
CAST(strftime('%d', col) AS INTEGER) |
DAY(col) |
extractWeek |
CAST(strftime('%W', col) AS INTEGER) |
WEEK(col) |
extractMonth |
CAST(strftime('%m', col) AS INTEGER) |
MONTH(col) |
extractYear |
CAST(strftime('%Y', col) AS INTEGER) |
YEAR(col) |
extractQuarter |
((CAST(strftime('%m', col) AS INTEGER) - 1) / 3) + 1 |
QUARTER(col) |
extractMinute |
CAST(strftime('%M', col) AS INTEGER) |
MINUTE(col) |
extractSecond |
CAST(strftime('%S', col) AS INTEGER) |
SECOND(col) |
addDays |
datetime(col, '+N days') |
DATE_ADD(col, INTERVAL N DAY) |
subtractDays |
datetime(col, '-N days') |
DATE_SUB(col, INTERVAL N DAY) |
dateDiffDays |
CAST((julianday(c1) - julianday(c2)) AS INTEGER) |
DATEDIFF(c1, c2) |
dateDiffHours |
(julianday(c1) - julianday(c2)) * 24 |
TIMESTAMPDIFF(HOUR, c2, c1) |
Security
All $column parameters are validated against the pattern [a-zA-Z0-9_.]+ before being interpolated into SQL. Passing an invalid column name (e.g. user-supplied input) throws an InvalidArgumentException. Never pass raw user input as a column name.
Known Limitations
Week Number Semantics
The dateTruncWeek() and extractWeek() methods produce slightly different week numbers between SQLite and MySQL:
| Driver | dateTruncWeek format |
extractWeek function |
Week start |
|---|---|---|---|
| SQLite | strftime('%W') — Monday-based, 00–53 |
strftime('%W') — Monday-based, 00–53 |
Monday |
| MySQL | DATE_FORMAT('%u') — Monday-based, 01–53 |
WEEK() — mode 0, Sunday-based, 0–53 |
Varies |
If exact cross-driver parity is required for week numbers, consider using dateTruncDay() and computing week buckets in application code.
dateFormat() Throws on Invalid Periods
The dateFormat() dispatcher throws an InvalidArgumentException if the period is not one of: hour, day, week, month, year. Validate user input before passing it to this method.
API
| Method | Description |
|---|---|
DatabaseExpressions::dateTruncHour(string $column): string |
SQL expression for hourly time bucket |
DatabaseExpressions::dateTruncDay(string $column): string |
SQL expression for daily time bucket |
DatabaseExpressions::dateTruncWeek(string $column): string |
SQL expression for weekly time bucket |
DatabaseExpressions::dateTruncMonth(string $column): string |
SQL expression for monthly time bucket |
DatabaseExpressions::dateTruncYear(string $column): string |
SQL expression for yearly time bucket |
DatabaseExpressions::dateFormat(string $column, string $period): string |
General dispatcher for date truncation; throws on invalid period |
DatabaseExpressions::extractHour(string $column): string |
Extract hour of day as integer (0–23) |
DatabaseExpressions::extractDay(string $column): string |
Extract day of month as integer (1–31) |
DatabaseExpressions::extractWeek(string $column): string |
Extract week number as integer (0–53) |
DatabaseExpressions::extractMonth(string $column): string |
Extract month as integer (1–12) |
DatabaseExpressions::extractYear(string $column): string |
Extract year as integer |
DatabaseExpressions::extractQuarter(string $column): string |
Extract quarter as integer (1–4) |
DatabaseExpressions::extractMinute(string $column): string |
Extract minute as integer (0–59) |
DatabaseExpressions::extractSecond(string $column): string |
Extract second as integer (0–59) |
DatabaseExpressions::addDays(string $column, int $days): string |
Add days to a datetime column |
DatabaseExpressions::subtractDays(string $column, int $days): string |
Subtract days from a datetime column |
DatabaseExpressions::dateDiffDays(string $col1, string $col2): string |
Difference between two date columns in whole days |
DatabaseExpressions::dateDiffHours(string $col1, string $col2): string |
Difference between two date columns in hours |
DatabaseExpressions::driver(): string |
Return the current DB driver name |
DatabaseExpressions::isSqlite(): bool |
Whether the current connection is SQLite |
Development
Support
If you find this project useful:
License
MIT
All versions of laravel-db-expressions with dependencies
illuminate/database Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0