Mastering Laravel's New Validation Rules and JSON Querying Methods
Better String Handling with Blank and Filled
Laravel's blank() and filled() helpers are essential for checking the state of your data. Previously, these helpers struggled when passed a Stringable object—the fluent string object returned by the str() helper. You had to manually cast the object back to a primitive string.
Now, the framework handles this natively. If you use str('Laravel'), you can pass that result directly into filled(), and it will correctly return true. This small refinement removes unnecessary casting and keeps your conditional logic clean.
Streamlined Array Validation with the Rule Class
Validating nested array data often feels like a chore, especially when mapping keys from an $enum->value. It gets messy fast.
Rule::array() method to solve this. Instead of a clunky array structure, you can chain the specific keys you expect. It accepts a list of arguments or a clean array, making your validation logic more readable and easier to maintain when dealing with dynamic form fields.
use Illuminate\Validation\Rule;
// New fluent approach
$request->validate([
'user' => Rule::array(['name', 'email']),
]);
Advanced JSON Querying with Overlaps
Handling whereJsonOverlaps method. While whereJsonContains is great for finding a single exact match, it fails when you need to check if a column contains any value from a given set.
Imagine a languages column storing ['en', 'fr']. If you search for either English or German, whereJsonContains won't return the record unless you perform multiple OR queries. whereJsonOverlaps solves this by checking if any element in your search array exists within the database array. It maps directly to native
// Returns records containing 'fr', 'en', or both
$podcasts = Podcast::query()
->whereJsonOverlaps('languages', ['fr', 'en'])
->get();
