Laravel Tutorials

Handling Request Data Presence in Laravel

Published
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

Sponsored

acquaintsoft logo
Acquaint Softtech

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

Visit Acquaint Softtech

The latest

View all →
Cancel In-Flight Form Submissions in Inertia.js v3.7 image

Cancel In-Flight Form Submissions in Inertia.js v3.7

Read article
Laravel Starter Kits Now Ship with Vite+ image

Laravel Starter Kits Now Ship with Vite+

Read article
Pessimistic Locking in Laravel Eloquent with refreshForUpdate() image

Pessimistic Locking in Laravel Eloquent with refreshForUpdate()

Read article
Mask Query Bindings in Laravel Exception Messages image

Mask Query Bindings in Laravel Exception Messages

Read article
whereBinary(): Case-Sensitive MySQL Queries in Laravel image

whereBinary(): Case-Sensitive MySQL Queries in Laravel

Read article
Compile PHP to Native Binaries with TypePHP image

Compile PHP to Native Binaries with TypePHP

Read article