The Blog

Laravel 5

Laravel 5

Laravel 5: Creating Combinations of Elements With crossJoin

John Koster
John Koster
April 11, 2018

The crossJoin method in Laravel's Illuminate\Support\Arr class allows you to combine input arrays, resulting in an array containing all possible combinations of the input values. Through an example, let's say we want to create a deck of cards. We can use crossJoin to combine arrays representing card suits and card values, resulting in a deck of 52 cards. We can further customize the deck by using array_map to transform the deck into a new array containing values in the format of <CARD_VALUE> of <CARD_SUITE>. Another example is using crossJoin to generate test payloads for browser testing, allowing you to create test cases for input element names and XSS payloads.

Laravel 5

Laravel 5: Determining if an Array Contains an Element With exists

John Koster
John Koster
April 11, 2018

Learn how to use the exists helper method in Laravel. This method allows you to check if a key or index exists in an array. It is similar to PHP's array_key_exists function. The method requires two parameters, the array and the key. You can use any value that returns true from the Arr::accessible helper method as the array value. Check out the example use cases to see it in action.

Laravel 5

Laravel 5: Determining if an Object is Array Accessible With accessible

John Koster
John Koster
April 11, 2018

The accessible helper method determines if a value is array accessible by checking if it is an array or an instance of ArrayAccess. The method returns true if the value is array accessible, and false otherwise.

Laravel 5

Laravel 5: Ensuring a Value is an Array With wrap

John Koster
John Koster
April 11, 2018

Learn about the wrap method in Laravel's Arr class, which allows you to easily convert a value to an array. See an example of how to use the wrap method to convert a non-array value to an array. Also, discover a global helper function called array_wrap that performs the same task as Arr::wrap.

Laravel 5

Laravel 5: Excluding Items From an Array With except

John Koster
John Koster
April 11, 2018

Learn how to use the except helper method in Laravel to retrieve key/value pairs from an array, excluding specified keys. Find out how to use the except method with single and multiple keys. Also, discover the global array_except helper function, which provides a shortcut for calling Arr::except.

Laravel 5

Laravel 5: Filtering Array Elements With only

John Koster
John Koster
April 11, 2018

The only method in Laravel can be used to retrieve a subset of an array based on the provided keys. It is the opposite of the except method. You can use the only helper method to retrieve specific values from an array, such as a user's first name. Additionally, there is a global array_only function that is a shortcut for calling Arr::only.

Laravel 5

Laravel 5: Finding the First Occurrence of an Element Matching a Condition With first

John Koster
John Koster
April 11, 2018

The first helper function in Laravel is used to find the first element in an array that satisfies a given condition. This condition is defined by a callback function that accepts a key and a value. If no element satisfies the condition, a default value can be provided. The function can be used with or without the callback parameter. Additionally, there is a global helper function called array_first which is a shortcut for calling Arr::first.

Laravel 5

Laravel 5: Finding the Last Occurrence of an Element Matching a Condition With last

John Koster
John Koster
April 11, 2018

The last helper method in Laravel is the opposite of the first method. It returns the last value that satisfies a given callback function or a default value if no value is found. The optional callback parameter allows you to define custom logic to determine if a value should be considered. There is also a global shortcut function array_last available for calling the last method.

Laravel 5

Laravel 5: Getting the First Element of an Array With head

John Koster
John Koster
April 11, 2018

The head function in PHP is a convenient way to get the first element of an array without modifying the array. It returns the first element or false if the array is empty. You can use the head function as an alternative to PHP's reset function.

Laravel 5

Laravel 5: Getting the Last Element of an Array With last

John Koster
John Koster
April 11, 2018

The last function in PHP is used to retrieve the last element of an array without modifying the array itself. It is the opposite of the head function. It can be used as a wrapper around PHP's end function. An example use case is to get the last element of an array, such as retrieving the value "sixth" from the array "fourth", "fifth", "sixth". This function can also be called using Laravel's last function or PHP's end function.

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