Home >Backend Development >PHP Tutorial >Request Handling with PHP Enums in Laravel

Request Handling with PHP Enums in Laravel

Johnathan Smith
Johnathan SmithOriginal
2025-03-05 15:33:09902browse

Request Handling with PHP Enums in Laravel

Laravel's enhanced request handling now directly supports enums, ensuring type safety when processing enumerated values from incoming requests. This streamlined approach simplifies input validation and guarantees data consistency for predefined value sets.

Let's illustrate with a subscription management system:

use App\Enums\UserRole;
// Basic enum retrieval
$role = $request->enum('role', UserRole::class);

Here's a practical example:

// app/Enums/SubscriptionTier.php
<?php namespace App\Enums;

enum SubscriptionTier: string
{
    case FREE = 'free';
    case BASIC = 'basic';
    case PRO = 'pro';
    case ENTERPRISE = 'enterprise';

    public function getMonthlyLimit(): int
    {
        return match($this) {
            self::FREE => 1000,
            self::BASIC => 5000,
            self::PRO => 20000,
            self::ENTERPRISE => 100000
        };
    }
}

// app/Controllers/AccountController.php
namespace App\Http\Controllers;

use App\Enums\SubscriptionTier;
use App\Models\Account;
use Illuminate\Http\Request;

class AccountController extends Controller
{
    public function updateSubscription(Request $request, Account $account)
    {
        $newTier = $request->enum('tier', SubscriptionTier::class);
        if (!$newTier) {
            return response()->json([
                'error' => 'Invalid subscription tier provided'
            ], 422);
        }
        $account->update([
            'subscription_tier' => $newTier,
            'monthly_limit' => $newTier->getMonthlyLimit(),
            'upgraded_at' => now()
        ]);

        return response()->json([
            'message' => 'Subscription updated successfully',
            'account' => $account->fresh()
        ]);
    }
}

Usage example:

// Valid request
{
    "tier": "pro"
}

// Successful response
{
    "message": "Subscription updated successfully",
    "account": {
        "id": 1,
        "subscription_tier": "pro",
        "monthly_limit": 20000,
        "upgraded_at": "2024-02-01T10:30:00.000000Z"
    }
}

// Invalid request
{
    "tier": "premium"  // Non-existent tier
}

// Error response
{
    "error": "Invalid subscription tier provided"
}

This enum-based request handling offers a robust, type-safe method for processing enumerated values, ensuring data integrity within your application.

The above is the detailed content of Request Handling with PHP Enums in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn