Handling Request Data Presence in Laravel

Last updated on by

Handling Request Data Presence in Laravel image

Laravel's whenHas method provides an elegant way to execute code based on input presence in requests. This feature is particularly useful for handling optional fields and conditional updates, reducing the need for repetitive presence checks.

This approach shines particularly in form submissions where certain fields trigger specific business logic - for example, when a user opts into email notifications, you might need to validate and store additional email-related preferences.

// Simple presence check
 
$request->whenHas('name', function ($name) {
// Process name when present
});

Here's an example of a notification preferences manager:

// app/Controllers/PreferencesController.php
<?php
 
namespace App\Http\Controllers;
 
use App\Models\UserPreferences;
use Illuminate\Http\Request;
 
class PreferencesController extends Controller
{
 
public function update(Request $request, UserPreferences $preferences)
 
{
$request->whenHas('email_frequency',
function ($frequency) use ($preferences) {
$preferences->update([
'email_frequency' => $frequency,
'last_email_update' => now()
]);
}
);
 
$request->whenHas('push_enabled',
function ($enabled) use ($preferences) {
$preferences->update([
'push_enabled' => $enabled,
'push_updated_at' => now()
]);
},
function () use ($preferences) {
$preferences->update([
'push_enabled' => false,
'push_updated_at' => now()
]);
}
);
 
return response()->json([
'message' => 'Preferences updated successfully',
'preferences' => $preferences->fresh()
]);
}
}

Example usage:

// Input with some preferences
 
{
"email_frequency": "weekly"
}
 
// Response
 
{
"message": "Preferences updated successfully",
"preferences": {
"email_frequency": "weekly",
"last_email_update": "2024-02-01T10:30:00.000000Z",
"push_enabled": false,
"push_updated_at": "2024-02-01T10:30:00.000000Z"
}
}

The whenHas method simplifies conditional request processing while maintaining clean, readable code structure.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell

The latest

View all →
Shift + AI = Fully Automated Laravel Upgrades image

Shift + AI = Fully Automated Laravel Upgrades

Read article
Laracon AU 2026 Announces Full Speaker Lineup, Schedule, and Workshops image

Laracon AU 2026 Announces Full Speaker Lineup, Schedule, and Workshops

Read article
Parsel: Parse PDFs, Office Documents, and Images in PHP image

Parsel: Parse PDFs, Office Documents, and Images in PHP

Read article
Typed Objects for Eloquent with Expressive image

Typed Objects for Eloquent with Expressive

Read article
Malware Blocking and Dependency Policies in Composer 2.10 image

Malware Blocking and Dependency Policies in Composer 2.10

Read article
Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects image

Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects

Read article