The Blog

Laravel

Laravel Fluent Part Two: The Public API

John Koster
John Koster
November 21, 2016

This article is part of a two part series covering the Laravel Fluent API. The first part introduces the API, while the second part focuses on the public API. The article discusses the get method which retrieves values from a Fluent instance, as well as the getAttributes, toArray, jsonSerialize, and toJson methods. It also explains how closures work with Fluent and demonstrates how to handle deeply nested data structures when using the toJson method.

Laravel

Laravel Macros: An Easy Way to Extend Laravel Components

John Koster
John Koster
November 21, 2016

Learn how to use macros in Laravel to add custom functionality and reduce code. Macros in Laravel are similar to extension methods in C#. For example, you can add a countWords macro to the Str support class. Macros can be used in both static and instance contexts and have access to all private and protected members of the class. The Macroable trait provides methods to create macros and check for their existence. A few Laravel components, such as the Eloquent Builder, also support macros.

Laravel

Laravel Miscellaneous Helper Function: data_fill

John Koster
John Koster
November 21, 2016

The data_fill helper function is similar to the data_set helper function, but it does not overwrite existing data. It internally calls the data_set function with the $overwrite parameter set to false. With data_fill, you can set data without worrying about overwriting existing values. The function calls data_fill and data_set in the provided example code would produce the same results.

Laravel

Laravel Miscellaneous Helper Function: data_get

John Koster
John Koster
November 21, 2016

Learn how to use the data_get function in PHP to retrieve values from arrays or objects. Find out how it differs from array_get and object_get and how it can be used with both arrays and objects as the target.

Laravel

Laravel Miscellaneous Helper Function: data_set

John Koster
John Koster
November 21, 2016

Learn how to use the data_set helper function in PHP to set the value of an item in an array or object, using dot notation. You can specify the $overwrite parameter to determine whether existing values should be replaced. See code examples for basic usage and setting nested properties. The function works the same way with objects as it does with arrays.

Laravel

Laravel Miscellaneous Helper Function: dd

John Koster
John Koster
November 21, 2016

The dd() function is a debugging utility in PHP that allows you to dump and display the values of variables. It will provide a human-readable representation of the values and halt the execution of the script. This function is useful for debugging and inspecting variable values during development.

Laravel

Laravel Miscellaneous Helper Function: object_get

John Koster
John Koster
November 21, 2016

The object_get function in PHP allows developers to retrieve properties from an object using dot notation. It is particularly useful when the existence of a property cannot be guaranteed. Examples demonstrate how to retrieve the department name from a sample object, and how to handle cases where a property doesn't exist or to provide a default value for non-existing properties.

Laravel

Laravel Miscellaneous Helper Function: tap

John Koster
John Koster
November 21, 2016

The tap helper function in Laravel allows you to call a callback function with a given value and return the original value. This is helpful when you want to perform multiple operations on a value in a method chain while preserving the original value. The tap function can be used with any type of value and is particularly useful in scenarios where you need to fix the return value of a series of method calls. It was introduced in Laravel 5.3 and can be easily integrated into older Laravel code bases.

Laravel

Laravel Miscellaneous Helper Function: trait_uses_recursive

John Koster
John Koster
November 21, 2016

The trait_uses_recursive function in PHP allows you to fetch an array of all the traits used by a given class or class instance. It includes the traits used by the class as well as any traits used by those traits. This function can be especially useful when you need to understand the structure and relationships between traits in your code.

Laravel

Laravel Miscellaneous Helper Function: value

John Koster
John Koster
November 21, 2016

The value function in this PHP code will return the default value of the supplied $value parameter. If the $value is an instance of the Closure class, the function will execute the closure and return its value. If not, the $value is simply returned. An example with an instance of Closure shows how the function returns the value returned by the closure, while an example without Closure returns the value directly.

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