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

tinkerwell logo
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article