Skip to content

Latest commit

 

History

History
300 lines (186 loc) · 4.2 KB

File metadata and controls

300 lines (186 loc) · 4.2 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');

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);

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 $expr, string $unit): CallableInterface

Add interval.

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

Db::subInterval(string $expr, 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');

Next Steps