The Blog

Laravel 5

Laravel 5

Laravel 5: Throwing HTTP Exceptions With abort

John Koster
John Koster
April 14, 2018

Learn how to use the abort function in Symfony to throw and handle HTTP exceptions. When called, the abort function will throw an instance of Symfony\Component\HttpKernel\Exception\HttpException with the provided code, message, and headers. If the code is 404, it will throw Symfony\Component\HttpKernel\Exception\NotFoundHttpException using only the user-supplied message. Check out the example to see how to use the abort function to handle unauthorized access with a 401 error code.

Laravel 5

Laravel 5: Triggering Events With event

John Koster
John Koster
April 14, 2018

The event helper function allows you to conveniently dispatch events and their listeners. You can dispatch events by their name or by instantiating a new instance of the event class. The function also supports object-based events without the need for an event dispatcher dependency. The function returns an array of responses from the listeners.

Laravel 5

Laravel 5: Adding a New Element to the Beginning of an Array With prepend

John Koster
John Koster
April 11, 2018

Learn how to use the prepend helper method in Laravel to add a new item to the beginning of an array. Control the key of the new item by specifying an optional parameter. The prepend method does not modify the original array and returns a modified copy.

Laravel 5

Laravel 5: Adding Elements to the Array with add

John Koster
John Koster
April 11, 2018

The add helper method allows you to add a key-value pair to an array if the key doesn't already exist. To use the add method, pass in the array, key, and value as arguments, and the method will return the updated array. Alternatively, you can use the global array_add function, which is a shortcut for calling Arr::add directly.

Laravel 5

Laravel 5: Adding or Setting New Array Element Values With set

John Koster
John Koster
April 11, 2018

The set method in Laravel is used to set values within an array, and it is the opposite of the forget method. This function modifies the original array. It uses dot notation for the key and can also be used to change the entire array by passing null as the key. There is also a global array_set helper function available as a shortcut to calling Arr::set.

Laravel 5

Laravel 5: Array Helper Functions

John Koster
John Koster
April 11, 2018

Learn about Laravel's helper functions for manipulating array data structures. These functions offer additional features beyond PHP's built-in array functions and are located within the "Illuminate\Support\Arr" static class. Most of these functions treat arrays as immutable, meaning changes return a copy of the original array while leaving the original array unchanged. Laravel's array functions also support "dot" notation for accessing items in an array, similar to JavaScript's dot notation for accessing object properties. Explore various Laravel array helper functions like adding elements, splitting arrays, collapsing multi-dimensional arrays, representing multi-dimensional arrays in dot notation, excluding and filtering array elements, reducing arrays, finding occurrences, checking if an array contains an element, adding or setting array element values, removing elements, and retrieving array values.

Laravel 5

Laravel 5: Checking if an Array Contains an Element With has

John Koster
John Koster
April 11, 2018

Learn how to use the has helper method in Laravel to check if a specific key exists in a nested array using dot notation. Discover how this method provides an easier and more readable alternative to using PHP's isset function. Additionally, find out how to use the global array_has helper function as a shortcut to calling the Arr::has method.

Laravel 5

Laravel 5: Checking if an Array is an Associative Array With isAssoc

John Koster
John Koster
April 11, 2018

The isAssoc helper method checks if a given array is an associative array. An array is considered associative if it does not have sequential numeric keys starting from zero. The method returns true if the array is associative, and false otherwise. The provided example demonstrates how the method determines if arrays are associative or not.

Laravel 5

Laravel 5: Collapsing a Multi-Dimensional Array to a Single Level With collapse

John Koster
John Koster
April 11, 2018

The collapse helper method in Laravel's Illuminate\Support\Arr class allows you to collapse a nested array into a single-level array. The method accepts an array as its parameter and returns a new array with the contents of all the nested arrays. The method can work with arrays that contain any data type. Keep in mind that the collapse method is not recursive, so it won't collapse arrays within nested arrays. However, the method can also be used on collections, including nested collections. Additionally, Laravel provides a global array_collapse helper function which is a shortcut to calling the Arr::collapse method.

Laravel 5

Laravel 5: Conditionally Retrieving Array Values With where

John Koster
John Koster
April 11, 2018

The where helper method in Laravel is used to filter an array based on a callback function. It iterates over each element in the array and executes the callback on each key-value pair. If the callback returns a value that evaluates to true, the key-value pair is kept in the filtered array. This method can be used to compare both array values and key-value pairs. Additionally, Laravel provides a global array_where helper function, which is a shortcut to calling Arr::where.

Latest posts

That Escalated Quickly: All the New Things

The past six months or so have been incredibly busy. What started as a new article series about cust...

Read more
Troubleshooting a Statamic Illegal Offset Type Error when Viewing Collection Entries in the Control Panel

In this post I talk about how I resolved a mysterious illegal offset type error when viewing collect...

Read more
Creating Simple HTTP Redirect Routes from a Statamic Site

Generating a custom Laravel routes file from a Statamic website to redirect to a new domain.

Read more
Disabling Vite File Hashes

Disabling file hashes in Vite output can be accomplished by modifying your project's vite.config.js

Read more
Implementing a Custom Laravel Blade Precompiler for Volt and Livewire

Learn how to implement a custom component compiler for Laravel's Blade templating language in this p...

Read more
Creating a Hybrid Cache System for Statamic: Part Five

Part 5 of 6 covers implementing a cache namespace and labeling system, which we can use to target mu...

Read more