Checking Context Availability in Laravel

Checking Context Availability in Laravel

Context validation in Laravel gets simpler with the new missing() and missingHidden() methods, providing clean boolean checks for key existence.

When working with Laravel's Context service, you often need to check if specific keys exist before using them. The new missing() and missingHidden() methods provide a straightforward way to verify context availability.

// Add values to context
Context::add('url', $request->url());
Context::addHidden('token', $request->bearerToken());

// Check if regular keys are missing
Context::missing('url');         // false
Context::missing('missing_key'); // true

// Check if hidden keys are missing
Context::missingHidden('url');   // true (it's not a hidden key)
Context::missingHidden('token'); // false

These methods provide a cleaner alternative to negating has() checks, making your code more readable when checking for missing context values.

If this guide was helpful to you, subscribe to my daily newsletter and give me a follow on X/Twitter and Bluesky. It helps a lot!

Subscribe to Harris Raftopoulos

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe