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 →
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article
Black Friday: Learn the habits behind AI workflows that work image

Black Friday: Learn the habits behind AI workflows that work

Read article
Laravel Black Friday and Cyber Monday Deals 2026 image

Laravel Black Friday and Cyber Monday Deals 2026

Read article
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article