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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-db-expressions

Laravel DB Expressions

Tests Latest Version on Packagist Last updated

Database-agnostic SQL expression helper for date truncation, extraction, and differences across SQLite and MySQL.

Requirements

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:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT


All versions of laravel-db-expressions with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/database Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package philiprehberger/laravel-db-expressions contains the following files

Loading the files please wait ...