I'm currently available for full time hire! Inquire Here

Laravel Helpers Documentation

🎉 Enjoying this package? Consider sponsoring me on GitHub or buying me a beer.

Usage

Arrays

array_accessible

1/**
2 * Determine whether the given value is array accessible.
3 *
4 * @param mixed $value
5 *
6 * @return bool
7 */
8function array_accessible($value): bool

array_add

1/**
2 * Add an element to an array using "dot" notation if it doesn't exist.
3 *
4 * @param array $array
5 * @param string $key
6 * @param mixed $value
7 *
8 * @return array
9 */
10function array_add(array $array, string $key, $value): array

array_collapse

1/**
2 * Collapse an array of arrays into a single array.
3 *
4 * @param iterable $array
5 *
6 * @return array
7 */
8function array_collapse(iterable $array): array

array_cross_join

1/**
2 * Cross join the given arrays, returning all possible permutations.
3 *
4 * @param iterable ...$arrays
5 *
6 * @return array
7 */
8function array_cross_join(...$arrays): array

array_divide

1/**
2 * Divide an array into two arrays. One with keys and the other with values.
3 *
4 * @param array $array
5 *
6 * @return array
7 */
8function array_divide(array $array): array

array_dot

1/**
2 * Flatten a multi-dimensional associative array with dots.
3 *
4 * @param iterable $array
5 * @param string $prepend
6 *
7 * @return array
8 */
9function array_dot(iterable $array, string $prepend = ''): array

array_except

1/**
2 * Get all of the given array except for a specified array of keys.
3 *
4 * @param array $array
5 * @param array|string $keys
6 *
7 * @return array
8 */
9function array_except(array $array, $keys): array

array_exists

1/**
2 * Determine if the given key exists in the provided array.
3 *
4 * @param \ArrayAccess|array $array
5 * @param string|int $key
6 *
7 * @return bool
8 */
9function array_exists($array, $key): bool

array_first

1/**
2 * Return the first element in an array passing a given truth test.
3 *
4 * @param iterable $array
5 * @param callable|null $callback
6 * @param mixed $default
7 *
8 * @return mixed
9 */
10function array_first(iterable $array, callable $callback = null, $default = null)

array_last

1/**
2 * Return the last element in an array passing a given truth test.
3 *
4 * @param array $array
5 * @param callable|null $callback
6 * @param mixed $default
7 *
8 * @return mixed
9 */
10function array_last(array $array, callable $callback = null, $default = null)

array_flatten

1/**
2 * Flatten a multi-dimensional array into a single level.
3 *
4 * @param iterable $array
5 * @param int $depth
6 *
7 * @return array
8 */
9function array_flatten(iterable $array, int $depth): array

array_forget

1/**
2 * Remove one or many array items from a given array using "dot" notation.
3 *
4 * @param array $array
5 * @param array|string $keys
6 *
7 * @return void
8 */
9function array_forget(array &$array, $keys)

array_get

1/**
2 * Get an item from an array using "dot" notation.
3 *
4 * @param \ArrayAccess|array $array
5 * @param string|int|null $key
6 * @param mixed $default
7 *
8 * @return mixed
9 */
10function array_get($array, $key, $default = null)

array_has

1/**
2 * Check if an item or items exist in an array using "dot" notation.
3 *
4 * @param \ArrayAccess|array $array
5 * @param string|array $keys
6 *
7 * @return bool
8 */
9function array_has($array, $keys): bool

array_has_any

1 /**
2 * Determine if any of the keys exist in an array using "dot" notation.
3 *
4 * @param \ArrayAccess|array $array
5 * @param string|array $keys
6 *
7 * @return bool
8 */
9function array_has_any($array, $keys): bool

array_is_assoc

1/**
2 * Determines if an array is associative.
3 *
4 * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
5 *
6 * @param array $array
7 *
8 * @return bool
9 */
10function array_is_assoc(array $array): bool

array_only

1/**
2 * Get a subset of the items from the given array.
3 *
4 * @param array $array
5 * @param array|string $keys
6 *
7 * @return array
8 */
9function array_only(array $array, $keys): array

array_pluck

1/**
2 * Pluck an array of values from an array.
3 *
4 * @param iterable $array
5 * @param string|array|int|null $value
6 * @param string|array|null $key
7 *
8 * @return array
9 */
10function array_pluck(iterable $array, $value, $key = null): array

array_prepend

1/**
2 * Push an item onto the beginning of an array.
3 *
4 * @param array $array
5 * @param mixed $value
6 * @param mixed $key
7 *
8 * @return array
9 */
10function array_prepend(array $array, $value, $key = null): array

array_pull

1/**
2 * Get a value from the array, and remove it.
3 *
4 * @param array $array
5 * @param string $key
6 * @param mixed $default
7 *
8 * @return mixed
9 */
10function array_pull(array &$array, string $key, $default = null)

array_query

1/**
2 * Convert the array into a query string.
3 *
4 * @param array $array
5 *
6 * @return string
7 */
8function array_query(array $array): string

array_random

1/**
2 * Get one or a specified number of random values from an array.
3 *
4 * @param array $array
5 * @param int|null $number
6 * @param bool|false $preserveKeys
7 *
8 * @return mixed
9 *
10 * @throws \InvalidArgumentException
11 */
12function array_random(array $array, int $number = null, bool $preserveKeys)

array_set

1/**
2 * Set an array item to a given value using "dot" notation.
3 *
4 * If no key is given to the method, the entire array will be replaced.
5 *
6 * @param array $array
7 * @param string|null $key
8 * @param mixed $value
9 *
10 * @return array
11 */
12function array_set(array &$array, ?string $key, $value): array

array_shuffle

1 /**
2 * Shuffle the given array and return the result.
3 *
4 * @param array $array
5 * @param int|null $seed
6 *
7 * @return array
8 */
9function array_shuffle(array $array, int $seed = null): array

array_sort_recursive

1/**
2 * Recursively sort an array by keys and values.
3 *
4 * @param array $array
5 * @param int $options
6 * @param bool $descending
7 *
8 * @return array
9 */
10function array_sort_recursive(array $array, int $options = SORT_REGULAR, bool $descending): array

array_to_css_classes

1/**
2 * Conditionally compile classes from an array into a CSS class list.
3 *
4 * @param array $array
5 *
6 * @return string
7 */
8function array_to_css_classes(array $array): string

array_where

1/**
2 * Filter the array using the given callback.
3 *
4 * @param array $array
5 * @param callable $callback
6 *
7 * @return array
8 */
9function array_where(array $array, callable $callback): array

array_wrap

1/**
2 * If the given value is not an array and not null, wrap it in one.
3 *
4 * @param mixed $value
5 *
6 * @return array
7 */
8function array_wrap($value): array

data_fill

1/**
2 * Fill in data where it's missing.
3 *
4 * @param mixed $target
5 * @param string|array $key
6 * @param mixed $value
7 * @return mixed
8 */
9function data_fill(&$target, $key, $value)

data_get

1/**
2 * Get an item from an array or object using "dot" notation.
3 *
4 * @param mixed $target
5 * @param string|array|int|null $key
6 * @param mixed $default
7 * @return mixed
8 */
9function data_get($target, $key, $default = null)

data_set

1/**
2 * Set an item on an array or object using dot notation.
3 *
4 * @param mixed $target
5 * @param string|array $key
6 * @param mixed $value
7 * @param bool $overwrite
8 *
9 * @return mixed
10 */
11function data_set(&$target, $key, $value, bool $overwrite = true)

head

1/**
2 * Get the first element of an array. Useful for method chaining.
3 *
4 * @param array $array
5 *
6 * @return mixed
7 */
8function head(array $array)

last

1/**
2 * Get the last element from an array.
3 *
4 * @param array $array
5 *
6 * @return mixed
7 */
8function last(array $array)

to_array

1/**
2 * Convert Json into Array.
3 *
4 * @param string $json
5 *
6 * @return array
7 */
8function to_array(string $json)

Strings

preg_replace_array

1/**
2 * Replace a given pattern with each value in the array in sequentially.
3 *
4 * @param string $pattern
5 * @param array $replacements
6 * @param string $subject
7 *
8 * @return string
9 */
10function preg_replace_array(string $pattern, array $replacements, string $subject): string

str_after

1/**
2 * Return the remainder of a string after the first occurrence of a given value.
3 *
4 * @param string $subject
5 * @param string $search
6 *
7 * @return string
8 */
9function str_after(string $subject, string $search): string

str_after_last

1/**
2 * Return the remainder of a string after the last occurrence of a given value.
3 *
4 * @param string $subject
5 * @param string $search
6 *
7 * @return string
8 */
9function str_after_last(string $subject, string $search): string

str_before

1/**
2 * Get the portion of a string before the first occurrence of a given value.
3 *
4 * @param string $subject
5 * @param string $search
6 *
7 * @return string
8 */
9function str_before(string $subject, string $search): string

str_before_last

1/**
2 * Get the portion of a string before the last occurrence of a given value.
3 *
4 * @param string $subject
5 * @param string $search
6 *
7 * @return string
8 */
9function str_before_last(string $subject, string $search): string

str_between

1/**
2 * Get the portion of a string between two given values.
3 *
4 * @param string $subject
5 * @param string $from
6 * @param string $to
7 *
8 * @return string
9 */
10function str_between(string $subject, string $from, string $to): string

str_contains

1 /**
2 * Determine if a given string contains a given substring.
3 *
4 * @param string $haystack
5 * @param string|string[] $needles
6 *
7 * @return bool
8 */
9function str_contains(string $haystack, $needles): bool

str_contains_all

1/**
2 * Determine if a given string contains all array values.
3 *
4 * @param string $haystack
5 * @param string[] $needles
6 *
7 * @return bool
8 */
9function str_contains_all(string $haystack, array $needles): bool

str_ends_with

1/**
2 * Determine if a given string ends with a given substring.
3 *
4 * @param string $haystack
5 * @param string|string[] $needles
6 *
7 * @return bool
8 */
9function str_ends_with(string $haystack, $needles): bool

str_finish

1/**
2 * Cap a string with a single instance of a given value.
3 *
4 * @param string $value
5 * @param string $cap
6 *
7 * @return string
8 */
9function str_finish(string $value, string $cap): string

str_is

1/**
2 * Determine if a given string matches a given pattern.
3 *
4 * @param string|array $pattern
5 * @param string $value
6 *
7 * @return bool
8 */
9function str_is($pattern, string $value): bool

str_is_uuid

1/**
2 * Determine if a given string is a valid UUID.
3 *
4 * @param string $value
5 *
6 * @return bool
7 */
8function str_is_uuid(string $value): bool

str_kebab

1/**
2 * Convert a string to kebab case.
3 *
4 * @param string $value
5 *
6 * @return string
7 */
8function str_kebab(string $value): string

str_length

1/**
2 * Return the length of the given string.
3 *
4 * @param string $value
5 * @param string|null $encoding
6 *
7 * @return int
8 */
9function str_length(string $value, string $encoding = null): int

str_limit

1/**
2 * Limit the number of characters in a string.
3 *
4 * @param string $value
5 * @param int $limit
6 * @param string $end
7 *
8 * @return string
9 */
10function str_limit(string $value, int $limit = 100, string $end = '...'): string

str_lower

1/**
2 * Convert the given string to lower-case.
3 *
4 * @param string $value
5 *
6 * @return string
7 */
8function str_lower(string $value): string

str_words

1/**
2 * Limit the number of words in a string.
3 *
4 * @param string $value
5 * @param int $words
6 * @param string $end
7 *
8 * @return string
9 */
10function str_words(string $value, int $words = 100, string $end = '...'): string

str_match

1/**
2 * Get the string matching the given pattern.
3 *
4 * @param string $pattern
5 * @param string $subject
6 *
7 * @return string
8 */
9function str_match(string $pattern, string $subject): string

str_pad_both

1/**
2 * Pad both sides of a string with another.
3 *
4 * @param string $value
5 * @param int $length
6 * @param string $pad
7 *
8 * @return string
9 */
10function str_pad_both(string $value, int $length, string $pad = ' '): string

str_pad_left

1/**
2 * Pad the left side of a string with another.
3 *
4 * @param string $value
5 * @param int $length
6 * @param string $pad
7 *
8 * @return string
9 */
10function str_pad_left(string $value, int $length, string $pad = ' '): string

str_pad_right

1/**
2 * Pad the right side of a string with another.
3 *
4 * @param string $value
5 * @param int $length
6 * @param string $pad
7 *
8 * @return string
9 */
10function str_pad_right(string $value, int $length, string $pad = ' '): string

str_random

1/**
2 * Generate a more truly "random" alpha-numeric string.
3 *
4 * @param int $length
5 *
6 * @return string
7 */
8function str_random(int $length = 16): string

str_replace_array

1/**
2 * Replace a given value in the string sequentially with an array.
3 *
4 * @param string $search
5 * @param array<int|string, string> $replace
6 * @param string $subject
7 *
8 * @return string
9 */
10function str_replace_array(string $search, array $replace, string $subject): string

str_replace_first

1/**
2 * Replace the first occurrence of a given value in the string.
3 *
4 * @param string $search
5 * @param string $replace
6 * @param string $subject
7 *
8 * @return string
9 */
10function str_replace_first(string $search, string $replace, string $subject): string

str_replace_last

1/**
2 * Replace the last occurrence of a given value in the string.
3 *
4 * @param string $search
5 * @param string $replace
6 * @param string $subject
7 *
8 * @return string
9 */
10function str_replace_last(string $search, string $replace, string $subject): string

str_remove

1/**
2 * Remove any occurrence of the given string in the subject.
3 *
4 * @param string|array<string> $search
5 * @param string $subject
6 * @param bool $caseSensitive
7 *
8 * @return string
9 */
10function str_remove($search, string $subject, bool $caseSensitive = true): string

str_start

1/**
2 * Begin a string with a single instance of a given value.
3 *
4 * @param string $value
5 * @param string $prefix
6 *
7 * @return string
8 */
9function str_start(string $value, string $prefix): string

str_upper

1/**
2 * Convert the given string to upper-case.
3 *
4 * @param string $value
5 *
6 * @return string
7 */
8function str_upper(string $value): string

str_title

1/**
2 * Convert the given string to title case.
3 *
4 * @param string $value
5 *
6 * @return string
7 */
8function str_title(string $value): string

str_snake

1/**
2 * Convert a string to snake case.
3 *
4 * @param string $value
5 * @param string $delimiter
6 *
7 * @return string
8 */
9function str_snake(string $value, string $delimiter = '_'): string

str_starts_with

1/**
2 * Determine if a given string starts with a given substring.
3 *
4 * @param string $haystack
5 * @param string|string[] $needles
6 *
7 * @return bool
8 */
9function str_starts_with(string $haystack, $needles): bool

str_studly

1/**
2 * Convert a value to studly caps case.
3 *
4 * @param string $value
5 *
6 * @return string
7 */
8function str_studly(string $value): string

str_pascal

1/**
2 * Convert a string to pascal case.
3 *
4 * @param string $value
5 *
6 * @return string
7 */
8function str_pascal(string $value): string

str_camel

1/**
2 * Convert a string to cameel case.
3 *
4 * @param string $value
5 *
6 * @return string
7 */
8function str_camel(string $value): string

str_uuid4

1/**
2 * Generate a UUID (version 4).
3 *
4 * @return string
5 */
6function str_uuid4(): string

str_jwt

1/**
2 * Generate a JWT.
3 *
4 * @param array $payload
5 *
6 * @return string
7 */
8function str_jwt(array $payload): string

Classes

class_basename

1/**
2 * Get the class "basename" of the given object / class.
3 *
4 * @param string|object $class
5 * @return string
6 */
7function class_basename($class): string

class_uses_recursive

1/**
2 * Returns all traits used by a class, its parent classes and trait of their traits.
3 *
4 * @param object|string $class
5 * @return array
6 */
7function class_uses_recursive($class): array

trait_uses_recursive

1/**
2 * Returns all traits used by a trait and its traits.
3 *
4 * @param string $trait
5 * @return array
6 */
7function trait_uses_recursive($trait): array

Miscellaneous

dd

1/**
2 * Dump the passed variables and end the script.
3 *
4 * @param mixed
5 * @return void
6 */
7function dd()

e

1/**
2 * Escape HTML entities in a string.
3 *
4 * @param string $value
5 *
6 * @return string
7 */
8function e(string $value): string

object_get

1 /**
2 * Get an item from an object using "dot" notation.
3 *
4 * @param object $object
5 * @param string $key
6 * @param mixed $default
7 *
8 * @return mixed
9 */
10function object_get(object $object, string $key, $default = null)

tap

1/**
2 * Call the given Closure with the given value then return the value.
3 *
4 * @param mixed $value
5 * @param callable $callback
6 *
7 * @return mixed
8 */
9function tap($value, callable $callback)

value

1/**
2 * Return the default value of the given value.
3 *
4 * @param mixed $value
5 * @return mixed
6 */
7function value($value)

with

1/**
2 * Return the given object. Useful for chaining.
3 *
4 * @param mixed $object
5 * @return mixed
6 */
7function with($object)