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

laravelcloud logo
Laravel Cloud

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

Visit Laravel Cloud

The latest

View all →
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article
Flock: A Free, Native Herd Alternative for Laravel image

Flock: A Free, Native Herd Alternative for Laravel

Read article
Live Props for Real-Time Updates in Inertia.js v3.11 image

Live Props for Real-Time Updates in Inertia.js v3.11

Read article
Cooldown Dependency Policy now in Composer 2.11 image

Cooldown Dependency Policy now in Composer 2.11

Read article
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article