Download the PHP package halimzidoune/query-macro-helper without Composer

On this page you can find all versions of the php package halimzidoune/query-macro-helper. 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 query-macro-helper

Laravel Query Macro Helper

A Laravel package that extends the Query Builder with database-agnostic SELECT macros. Transform your raw SQL expressions into readable, chainable methods that automatically generate the correct syntax for each database driver. Replace complex DB::raw() statements with intuitive methods like selectConcat(), selectDateFormat(), and selectCase() that work seamlessly across MySQL, SQLite, PostgreSQL, Oracle, SQL Server.

🚀 Features

✨ Benefits

📦 Installation

Via Composer

Service Provider Registration

Add this to your config/app.php:

For laravel > 10 bootstrap/providers.php:

It's recomended to add this line in AppSeriveProvider@boot for a better cast:

✍️ Writing a custom macro

Use the generator, then implement driver-aware SQL as needed.

This creates app/Builders/Macros/Lower.php. Example implementation:

Usage:

Example 2: Driver-specific implementation

Here's a more complex example that shows how to handle different database drivers:

Usage:

Note: When creating custom macros that accept multiple arguments, consider using str() helper for literal strings to distinguish them from column names, similar to how selectConcat works.

More ideas you could implement:

📚 Macro reference (quick table)

Below are the macros shipped with the package, grouped by category. Call these as chained methods on Query\Builder or Eloquent\Builder. Aliases can be provided using "column as alias".

String

Macro Purpose
selectConcat Concatenate columns/values
selectUpper Uppercase text
selectLower Lowercase text
selectLength String length
selectSubstring Substring by start/length
selectReplace Replace substring
selectTrim Trim whitespace
selectPad Pad string to length
selectStartsWith Starts-with check
selectEndsWith Ends-with check
selectContains Contains check
selectRegexp Regex match flag/value
selectSlug URL-friendly slug
selectCase SQL CASE mapping

Number

Macro Purpose
selectAdd Addition
selectSubtract Subtraction
selectMultiply Multiplication
selectAbs Absolute value
selectRound Round to decimals
selectFloor Floor
selectCeil Ceil
selectPower Power/exponent
selectSqrt Square root
selectModulo Modulo/remainder
selectPercent Percentage of total
selectTruncate Truncate decimals
selectRandom Random number
selectRandomBetween Random in range
selectSafeDivision Divide with zero-safe fallback

Datetime

Macro Purpose
selectDateFormat Format date/time with Carbon-like tokens
selectStartOfDay Start of day
selectEndOfDay End of day
selectStartOfWeek Start of ISO week
selectEndOfWeek End of ISO week
selectStartOfYear Start of year
selectEndOfYear End of year
selectStartOfHour Start of hour
selectEndOfHour End of hour
selectDayOfWeek Day of week number
selectWeekOfYear ISO week number
selectDaysInMonth Days count in month
selectAge Age from date
selectDiffInDays Difference in days
selectDiffInMinutes Difference in minutes
selectDiffInSeconds Difference in seconds
selectAddTime Add interval to datetime
selectIsSameDay Same calendar day?
selectIsSameYear Same calendar year?
selectIsSameHour Same hour?
selectIsSameMinute Same minute?
selectEndOfMonth End of month

Casts

Macro Purpose
selectString Cast to string (varchar/text)
selectInteger Cast to integer
selectFloat Cast to float/decimal
selectBoolean Cast to boolean
selectDate Cast to date
selectDateTime Cast to datetime

🔧 Available Macros

selectConcat(result_alias, column1, ...)

Concatenates multiple columns or values with database-specific syntax.

Important: Use str() helper for literal strings to distinguish them from column names.

More examples:

selectUpper(column)

Converts text to uppercase.

selectLower(column)

Converts text to lowercase.

selectLength(column)

Gets the length of a string.

selectSubstring(column, start, length)

Extracts a substring from a string.

selectReplace(column, search, replace)

Replaces occurrences of a substring.

selectTrim(column)

Removes leading and trailing whitespace.

selectPad(column, length, pad_string, pad_type)

Pads a string to a specified length.

selectStartsWith(column, prefix)

Checks if a string starts with a specific prefix.

selectEndsWith(column, suffix)

Checks if a string ends with a specific suffix.

selectContains(column, substring)

Checks if a string contains a substring.

selectRegexp(column, pattern)

Performs regex pattern matching.

selectSlug(column)

Converts text to URL-friendly slug format.

selectCase(column, cases, default)

Performs CASE statement operations.

Number Operations

selectAdd(column1, column2)

Adds two numeric values.

selectSubtract(column1, column2)

Subtracts one numeric value from another.

selectMultiply(column1, column2)

Multiplies two numeric values.

selectRandom()

Generates a random number (database-specific implementation).

selectRandomBetween(min, max)

Generates a random number within a range.

selectAbs(column)

Returns the absolute value of a number.

selectRound(column, decimals)

Rounds a number to a specified number of decimal places.

selectFloor(column)

Rounds a number down to the nearest integer.

selectCeil(column)

Rounds a number up to the nearest integer.

selectPower(column, exponent)

Raises a number to a specified power.

selectSqrt(column)

Calculates the square root of a number.

selectModulo(column, divisor)

Returns the remainder of division.

selectPercent(column, total)

Calculates percentage.

selectTruncate(column, decimals)

Truncates a number to a specified number of decimal places.

selectSafeDivision(column1, column2, default)

Performs safe division with fallback for division by zero.

DateTime Operations

selectDateFormat(column, format)

Formats a date using Carbon-style format strings.

selectStartOfDay(column)

Gets the start of day for a date.

selectEndOfDay(column)

Gets the end of day for a date.

selectStartOfWeek(column)

Gets the start of week for a date.

selectEndOfWeek(column)

Gets the end of week for a date.

selectEndOfMonth(column)

Gets the end of month for a date.

selectStartOfYear(column)

Gets the start of year for a date.

selectEndOfYear(column)

Gets the end of year for a date.

selectStartOfHour(column)

Gets the start of hour for a datetime.

selectEndOfHour(column)

Gets the end of hour for a datetime.

selectDayOfWeek(column)

Gets the day of week (1-7).

selectWeekOfYear(column)

Gets the week number of the year.

selectDaysInMonth(column)

Gets the number of days in a month.

selectAge(column)

Calculates age from a birth date.

selectDiffInDays(column1, column2)

Calculates the difference in days between two dates.

selectDiffInMinutes(column1, column2)

Calculates the difference in minutes between two datetimes.

selectDiffInSeconds(column1, column2)

Calculates the difference in seconds between two datetimes.

selectAddTime(column, amount, unit)

Adds time to a date/datetime.

selectIsSameDay(column1, column2)

Checks if two dates are the same day.

selectIsSameYear(column1, column2)

Checks if two dates are in the same year.

selectIsSameHour(column1, column2)

Checks if two datetimes are in the same hour.

selectIsSameMinute(column1, column2)

Checks if two datetimes are in the same minute.

selectIsLeapYear(column)

Checks if a year is a leap year.

Type Casting Operations

selectString(column, length)

Casts a value to string type.

selectInteger(column)

Casts a value to integer type.

selectFloat(column, precision, scale)

Casts a value to float type.

selectBoolean(column)

Casts a value to boolean type.

selectDate(column)

Casts a value to date type.

selectDateTime(column)

Casts a value to datetime type.

🛠️ Creating Custom Macros

Using Artisan Command

This will create a new macro class in app/Builders/Macros/ directory.

Manual Creation

Create a class that extends BaseMacro and implements the required methods:

🔍 Advanced Usage Examples

Complex Queries with Multiple Macros

Using with Eloquent Models

🌟 Supported Database Drivers

📚 Best Practices

  1. Use Descriptive Aliases: Always provide meaningful aliases for your macro results
  2. Combine with Existing Methods: Mix macros with standard Laravel query methods
  3. Performance Considerations: Some macros may be more efficient than others on specific databases
  4. Testing: Test your queries across different database drivers when possible

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

The MIT License (MIT). Please see License File for more information.

🆘 Support

If you encounter any issues or have questions, please open an issue on GitHub or contact the maintainer.


Made with ❤️ for the Laravel community


All versions of query-macro-helper with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/support Version ^10.0 || ^11.0
illuminate/database Version ^10.0 || ^11.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 halimzidoune/query-macro-helper contains the following files

Loading the files please wait ...