Helper Functions

Arrays

array_add

The array_add function adds a given key / value pair to the array if the given key doesn't already exist in the array.

  1. $array = array('foo' => 'bar');
  2. $array = array_add($array, 'key', 'value');

array_divide

The array_divide function returns two arrays, one containing the keys, and the other containing the values of the original array.

  1. $array = array('foo' => 'bar');
  2. list($keys, $values) = array_divide($array);

array_dot

The array_dot function flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth.

  1. $array = array('foo' => array('bar' => 'baz'));
  2. $array = array_dot($array);
  3. // array('foo.bar' => 'baz');

array_except

The array_except method removes the given key / value pairs from the array.

  1. $array = array_except($array, array('keys', 'to', 'remove'));

array_fetch

The array_fetch method returns a flattened array containing the selected nested element.

  1. $array = array(
  2. array('developer' => array('name' => 'Taylor')),
  3. array('developer' => array('name' => 'Dayle')),
  4. );
  5. $array = array_fetch($array, 'developer.name');
  6. // array('Taylor', 'Dayle');

array_first

The array_first method returns the first element of an array passing a given truth test.

  1. $array = array(100, 200, 300);
  2. $value = array_first($array, function($key, $value)
  3. {
  4. return $value >= 150;
  5. });

A default value may also be passed as the third parameter:

  1. $value = array_first($array, $callback, $default);

array_last

The array_last method returns the last element of an array passing a given truth test.

  1. $array = array(350, 400, 500, 300, 200, 100);
  2. $value = array_last($array, function($key, $value)
  3. {
  4. return $value > 350;
  5. });
  6. // 500

A default value may also be passed as the third parameter:

  1. $value = array_last($array, $callback, $default);

array_flatten

The array_flatten method will flatten a multi-dimensional array into a single level.

  1. $array = array('name' => 'Joe', 'languages' => array('PHP', 'Ruby'));
  2. $array = array_flatten($array);
  3. // array('Joe', 'PHP', 'Ruby');

array_forget

The array_forget method will remove a given key / value pair from a deeply nested array using "dot" notation.

  1. $array = array('names' => array('joe' => array('programmer')));
  2. array_forget($array, 'names.joe');

array_get

The array_get method will retrieve a given value from a deeply nested array using "dot" notation.

  1. $array = array('names' => array('joe' => array('programmer')));
  2. $value = array_get($array, 'names.joe');
  3. $value = array_get($array, 'names.john', 'default');

Note: Want something like array_get but for objects instead? Use object_get.

array_only

The array_only method will return only the specified key / value pairs from the array.

  1. $array = array('name' => 'Joe', 'age' => 27, 'votes' => 1);
  2. $array = array_only($array, array('name', 'votes'));

array_pluck

The array_pluck method will pluck a list of the given key / value pairs from the array.

  1. $array = array(array('name' => 'Taylor'), array('name' => 'Dayle'));
  2. $array = array_pluck($array, 'name');
  3. // array('Taylor', 'Dayle');

array_pull

The array_pull method will return a given key / value pair from the array, as well as remove it.

  1. $array = array('name' => 'Taylor', 'age' => 27);
  2. $name = array_pull($array, 'name');

array_set

The array_set method will set a value within a deeply nested array using "dot" notation.

  1. $array = array('names' => array('programmer' => 'Joe'));
  2. array_set($array, 'names.editor', 'Taylor');

array_sort

The array_sort method sorts the array by the results of the given Closure.

  1. $array = array(
  2. array('name' => 'Jill'),
  3. array('name' => 'Barry'),
  4. );
  5. $array = array_values(array_sort($array, function($value)
  6. {
  7. return $value['name'];
  8. }));

array_where

Filter the array using the given Closure.

  1. $array = array(100, '200', 300, '400', 500);
  2. $array = array_where($array, function($key, $value)
  3. {
  4. return is_string($value);
  5. });
  6. // Array ( [1] => 200 [3] => 400 )

head

Return the first element in the array. Useful for method chaining in PHP 5.3.x.

  1. $first = head($this->returnsArray('foo'));

last

Return the last element in the array. Useful for method chaining.

  1. $last = last($this->returnsArray('foo'));

Paths

app_path

Get the fully qualified path to the app directory.

  1. $path = app_path();

base_path

Get the fully qualified path to the root of the application install.

public_path

Get the fully qualified path to the public directory.

storage_path

Get the fully qualified path to the app/storage directory.

Strings

camel_case

Convert the given string to camelCase.

  1. $camel = camel_case('foo_bar');
  2. // fooBar

class_basename

Get the class name of the given class, without any namespace names.

  1. $class = class_basename('Foo\Bar\Baz');
  2. // Baz

e

Run htmlentities over the given string, with UTF-8 support.

  1. $entities = e('<html>foo</html>');

ends_with

Determine if the given haystack ends with a given needle.

  1. $value = ends_with('This is my name', 'name');

snake_case

Convert the given string to snake_case.

  1. $snake = snake_case('fooBar');
  2. // foo_bar

str_limit

Limit the number of characters in a string.

  1. str_limit($value, $limit = 100, $end = '...')

Example:

  1. $value = str_limit('The PHP framework for web artisans.', 7);
  2. // The PHP...

starts_with

Determine if the given haystack begins with the given needle.

  1. $value = starts_with('This is my name', 'This');

str_contains

Determine if the given haystack contains the given needle.

  1. $value = str_contains('This is my name', 'my');

str_finish

Add a single instance of the given needle to the haystack. Remove any extra instances.

  1. $string = str_finish('this/string', '/');
  2. // this/string/

str_is

Determine if a given string matches a given pattern. Asterisks may be used to indicate wildcards.

  1. $value = str_is('foo*', 'foobar');

str_plural

Convert a string to its plural form (English only).

  1. $plural = str_plural('car');

str_random

Generate a random string of the given length.

  1. $string = str_random(40);

str_singular

Convert a string to its singular form (English only).

  1. $singular = str_singular('cars');

studly_case

Convert the given string to StudlyCase.

  1. $value = studly_case('foo_bar');
  2. // FooBar

trans

Translate a given language line. Alias of Lang::get.

  1. $value = trans('validation.required'):

trans_choice

Translate a given language line with inflection. Alias of Lang::choice.

  1. $value = trans_choice('foo.bar', $count);

URLs

action

Generate a URL for a given controller action.

  1. $url = action('[email protected]', $params);

route

Generate a URL for a given named route.

  1. $url = route('routeName', $params);

asset

Generate a URL for an asset.

  1. $url = asset('img/photo.jpg');

Generate a HTML link to the given URL.

  1. echo link_to('foo/bar', $title, $attributes = array(), $secure = null);

Generate a HTML link to the given asset.

  1. echo link_to_asset('foo/bar.zip', $title, $attributes = array(), $secure = null);

Generate a HTML link to the given route.

  1. echo link_to_route('route.name', $title, $parameters = array(), $attributes = array());

Generate a HTML link to the given controller action.

  1. echo link_to_action('[email protected]', $title, $parameters = array(), $attributes = array());

secure_asset

Generate a HTML link to the given asset using HTTPS.

  1. echo secure_asset('foo/bar.zip', $title, $attributes = array());

secure_url

Generate a fully qualified URL to a given path using HTTPS.

  1. echo secure_url('foo/bar', $parameters = array());

url

Generate a fully qualified URL to the given path.

  1. echo url('foo/bar', $parameters = array(), $secure = null);

Miscellaneous

csrf_token

Get the value of the current CSRF token.

  1. $token = csrf_token();

dd

Dump the given variable and end execution of the script.

  1. dd($value);

value

If the given value is a Closure, return the value returned by the Closure. Otherwise, return the value.

  1. $value = value(function() { return 'bar'; });

with

Return the given object. Useful for method chaining constructors in PHP 5.3.x.

  1. $value = with(new Foo)->doWork();