20 Lesser Known Functions in Laravel

20 Lesser Known Functions in Laravel

Laravel is a popular PHP web application framework, widely used for web development. Laravel comes with many built-in functions that make development faster and easier. However, there are many lesser-known functions in Laravel that can make your development process even more efficient. In this blog post, we will explore 20 of these functions.

1. tap()

The tap() function allows you to perform actions on an object without needing to store it in a variable. This can be useful when working with complex objects or when you want to chain methods together. For instance, you can create an object and perform an action on it using the tap() function, and then chain another method onto the end of the tap() function. This can help to simplify your code and make it more readable.

$object = tap(new Object, function($object) {
    $object->doSomething();
})->doSomethingElse();

2. optional()

The optional() function allows you to access properties or methods on an object without needing to check if the object is null first. This function is useful when dealing with objects that may or may not exist. By using optional(), you can safely access the object's properties or methods without causing an error if the object is null.

$object = null;
$value = optional($object)->property;

3. value()

The value() function returns the value of a given variable. This can be useful when you want to ensure that a variable is not null before using it. You can use this function to check if a variable is null before using it in your code.

$value = value($variable);

4. filled()

The filled() function checks if a given value is not null or an empty string. This function is useful when validating user input or form data. You can use the filled() function to check if a field in the form is empty or null.

$isFilled = filled($value);

5. blank()

The blank() function checks if a given value is null or an empty string. This function is useful when validating user input or form data. You can use the blank() function to check if a field in the form is blank or null.

$isBlank = blank($value);

6. data_fill()

The data_fill() function sets a default value for a nested array element. This function is useful when working with complex arrays. You can use the data_fill() function to set a default value for a nested array element.

$data = ['user' => ['name' => 'John']];
$data = data_fill($data, 'user.address.city', 'New York');

7. data_get()

The data_get() function gets a value from a nested array element. This function is useful when working with complex arrays. You can use the data_get() function to get a value from a nested array element.

$data = ['user' => ['name' => 'John']];
$name = data_get($data, 'user.name');

8. data_set()

The data_set() function sets a value for a nested array element. This function is useful when working with complex arrays. You can use the data_set() function to set a value for a nested array element.

$data = ['user' => ['name' => 'John']];
$data = data_set($data, 'user.email', 'john@example.com');

9. head()

The head() function gets the first element of an array. This function is useful when working with arrays. You can use the head() function to get the first element of an array.

$first = head($array);

10. last()

The last() function gets the last element of an array. This function is useful when working with arrays. You can use the last() function to get the last element of an array.

$last = last($array);

11. array_add()

The array_add() function adds a key-value pair to an array if the key does not already exist. This function is useful when working with arrays. You can use the array_add() function to add a key-value pair to an array.

$array = array_add($array, 'key', 'value');

12. array_except()

The array_except() function removes a key-value pair from an array. This function is useful when working with arrays. You can use the array_except() function to remove a key-value pair from an array.

$array = array_except($array, 'key');

13. array_first()

The array_first() function returns the first item in an array that passes a given truth test. This function is useful when working with arrays. You can use the array_first() function to get the first item in an array that passes a given truth test.

$item = array_first($array, function ($value) {
    return $value > 5;
});

14. array_last()

The array_last() function returns the last item in an array that passes a given truth test. This function is useful when working with arrays. You can use the array_last() function to get the last item in an array that passes a given truth test.

$item = array_last($array, function ($value) {
    return $value > 5;
});

15. array_pluck()

The array_pluck() function gets the value of a given key from each array element. This function is useful when working with arrays. You can use the array_pluck() function to get the value of a given key from each array element.

$names = array_pluck($array, 'name');

16. array_prepend()

The array_prepend() function adds a value to the beginning of an array. This function is useful when working with arrays. You can use the array_prepend() function to add a value to the beginning of an array.

$array = array_prepend($array, 'value');

17. array_pull()

The array_pull() function removes and returns a value from an array. This function is useful when working with arrays. You can use the array_pull() function to remove and return a value from an array.

$value = array_pull($array, 'key');

18. array_sort()

The array_sort() function sorts an array by the given key. This function is useful when working with arrays. You can use the array_sort() function to sort an array by the given key.

$array = array_sort($array, 'key');

19. array_where()

The array_where() function filters an array by a given key-value pair. This function is useful when working with arrays. You can use the array_where() function to filter an array by a given key-value pair.

$array = array_where($array, function ($value, $key) {
    return $value > 5;
});

20. dispatch()

The dispatch() function dispatches a job onto the Laravel queue. This function is useful when working with Laravel. You can use the dispatch() function to dispatch a job onto the Laravel queue.

dispatch(new Job);

Conclusion

These are just a few of the lesser-known functions in Laravel that can help to simplify your code and make your development process more efficient. By taking advantage of these functions, you can write cleaner, more readable code that is easier to maintain.