Skip to content

Latest commit

 

History

History
428 lines (267 loc) · 6.44 KB

File metadata and controls

428 lines (267 loc) · 6.44 KB

Helper Functions Reference

Complete reference for all helper functions.

Core Helpers

Db::raw(string $value): RawValue

Raw SQL value.

Db::raw('NOW()');

Db::escape(string $value): string

Escape string.

Db::escape($userInput);

Db::config(string $name, $value): ValueInterface

Configuration value.

Db::config('FOREIGN_KEY_CHECKS', 0);

String Helpers

Db::concat(...$values): CallableInterface

CONCAT function.

Db::concat('first_name', ' ', 'last_name');

Db::upper(string $value): CallableInterface

UPPER function.

Db::upper('email');

Db::lower(string $value): CallableInterface

LOWER function.

Db::lower('email');

Db::trim(string $value, ?string $chars = null): CallableInterface

TRIM function.

Db::trim('name');

Db::substring(string $value, int $start, ?int $length = null): CallableInterface

SUBSTRING function.

Db::substring('name', 1, 10);

Db::length(string $value): CallableInterface

LENGTH function.

Db::length('name');

Db::left(string|RawValue $value, int $length): CallableInterface

Left substring.

Db::left('name', 2);

Db::right(string|RawValue $value, int $length): CallableInterface

Right substring.

Db::right('name', 2);

Db::position(string|RawValue $substring, string|RawValue $value): CallableInterface

Substring position (1-based).

Db::position(Db::raw("'@'"), 'email');

Db::repeat(string|RawValue $value, int $count): CallableInterface

Repeat string.

Db::repeat(Db::raw("'-'"), 5);

Db::reverse(string|RawValue $value): CallableInterface

Reverse string.

Db::reverse('name');

Db::padLeft(string|RawValue $value, int $length, string $padString = ' '): CallableInterface

Left pad.

Db::padLeft('name', 8, ' ');

Db::padRight(string|RawValue $value, int $length, string $padString = ' '): CallableInterface

Right pad.

Db::padRight('name', 8, '.');

Numeric Helpers

Db::inc(int|float $value, int|float $step = 1): CallableInterface

Increment value.

Db::inc('views');
Db::inc('views', 5);

Db::dec(int|float $value, int|float $step = 1): CallableInterface

Decrement value.

Db::dec('stock');

Db::abs(int|float $value): CallableInterface

Absolute value.

Db::abs('price');

Db::round(int|float $value, int $precision = 0): CallableInterface

Round value.

Db::round('price', 2);

Db::ceil(string|RawValue $value): CallableInterface

Ceiling (round up).

Db::ceil('price');

Db::floor(string|RawValue $value): CallableInterface

Floor (round down).

Db::floor('price');

Db::power(string|RawValue $value, string|int|float|RawValue $exponent): CallableInterface

Exponentiation.

Db::power('score', 2);

Db::sqrt(string|RawValue $value): CallableInterface

Square root.

Db::sqrt('distance');

Db::exp(string|RawValue $value): CallableInterface

Exponential function.

Db::exp(1);

Db::ln(string|RawValue $value): CallableInterface

Natural logarithm.

Db::ln('value');

Db::log(string|RawValue $value, string|int|float|RawValue|null $base = null): CallableInterface

Logarithm (base 10 by default).

Db::log('value');
Db::log('value', 2);

Db::trunc(string|RawValue $value, int $precision = 0): CallableInterface

Truncate without rounding.

Db::trunc('price', 1);

Date Helpers

Db::now(): RawValue

Current timestamp.

Db::now();

Db::curDate(): RawValue

Current date.

Db::curDate();

Db::curTime(): RawValue

Current time.

Db::curTime();

Db::addInterval(string|RawValue $expr, string $value, string $unit): CallableInterface

Add interval.

Db::addInterval('created_at', '1', 'DAY');

Db::subInterval(string|RawValue $expr, string $value, string $unit): CallableInterface

Subtract interval.

Db::subInterval('created_at', '1', 'MONTH');

NULL Helpers

Db::isNull($value): CallableInterface

IS NULL check.

Db::isNull('deleted_at');

Db::isNotNull($value): CallableInterface

IS NOT NULL check.

Db::isNotNull('email');

Db::ifNull($value, $default): CallableInterface

IFNULL function.

Db::ifNull('nickname', 'Anonymous');

Db::nullIf($value, $compare): CallableInterface

NULLIF function.

Db::nullIf('nickname', '');

Comparison Helpers

Db::like(string $column, string $pattern): CallableInterface

LIKE pattern.

Db::like('name', '%john%');

Db::between($column, $min, $max): CallableInterface

BETWEEN values.

Db::between('age', 18, 65);

Db::in(string $column, array $values): CallableInterface

IN values.

Db::in('status', ['active', 'pending']);

Db::notIn(string $column, array $values): CallableInterface

NOT IN values.

Db::notIn('status', ['deleted', 'banned']);

JSON Helpers

Db::jsonExtract(string $column, string $path): CallableInterface

Extract JSON value.

Db::jsonExtract('data', '$.name');

Db::jsonContains(string $column, $value, ?string $path = null): CallableInterface

Check JSON contains.

Db::jsonContains('tags', 'php');

Db::jsonSet(string $column, string $path, $value): CallableInterface

Set JSON value.

Db::jsonSet('data', '$.status', 'active');

Aggregate Helpers

Db::count(?string $column = '*'): CallableInterface

COUNT aggregate.

Db::count();
Db::count('id');

Db::sum(string $column): CallableInterface

SUM aggregate.

Db::sum('price');

Db::avg(string $column): CallableInterface

AVG aggregate.

Db::avg('price');

Db::min(string $column): CallableInterface

MIN aggregate.

Db::min('price');

Db::max(string $column): CallableInterface

MAX aggregate.

Db::max('price');

Db::groupConcat(string|RawValue $column, string $separator = ',', bool $distinct = false): CallableInterface

Concatenate values per group (dialect-specific: GROUP_CONCAT/STRING_AGG).

Db::groupConcat('name', ', ', true);

Next Steps