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

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Major performance improvements & security patches for Filament v4.12 and v5.7! image

Major performance improvements & security patches for Filament v4.12 and v5.7!

Read article
Laravel Boost Project Rules: Teach Agents Your Conventions image

Laravel Boost Project Rules: Teach Agents Your Conventions

Read article
Extract an Image's Dominant Color in Laravel image

Extract an Image's Dominant Color in Laravel

Read article
Image Dominant Color and HEIC Support in Laravel 13.24 image

Image Dominant Color and HEIC Support in Laravel 13.24

Read article
Official Laravel Zed Extension: LSP for PHP & Blade image

Official Laravel Zed Extension: LSP for PHP & Blade

Read article
Laravel Head: Manage Meta Tags, Open Graph, and JSON-LD image

Laravel Head: Manage Meta Tags, Open Graph, and JSON-LD

Read article