Laravel Integration
Complete guide for using Data Helpers with Laravel.
Introduction
Section titled “Introduction”Data Helpers provides seamless Laravel integration:
- ✅ Automatic Service Provider - Zero configuration
- ✅ Controller Injection - Automatic validation & filling
- ✅ Eloquent Integration - fromModel(), toModel()
- ✅ Request Validation - validateAndCreate()
- ✅ Artisan Commands - make:dto, dto:typescript
- ✅ Laravel Attributes - WhenAuth, WhenGuest, WhenCan, WhenRole
- ✅ API Resources - Replace Laravel Resources
Installation
Section titled “Installation”composer require event4u/data-helpersLaravel automatically discovers the service provider. No configuration needed!
Publish Configuration (Optional)
Section titled “Publish Configuration (Optional)”php artisan vendor:publish --tag=data-helpers-configController Injection
Section titled “Controller Injection”Automatic Validation & Injection
Section titled “Automatic Validation & Injection”Type-hint your DTO in controller methods:
use App\DTOs\UserRegistrationDTO;use Illuminate\Http\JsonResponse;
class UserController extends Controller{ public function register(UserRegistrationDTO $dto): JsonResponse { // $dto is automatically validated and filled with request data $user = User::create($dto->toArray());
return response()->json($user, 201); }}How It Works
Section titled “How It Works”- Laravel’s service container detects the DTO type hint
- Service provider creates an instance
- Request data is automatically passed to the DTO
- Validation runs automatically
- Controller receives the validated DTO
Manual Creation
Section titled “Manual Creation”public function register(Request $request): JsonResponse{ $dto = UserRegistrationDTO::fromRequest($request); $dto->validate(); // Throws ValidationException on failure
$user = User::create($dto->toArray()); return response()->json($user, 201);}Eloquent Integration
Section titled “Eloquent Integration”From Eloquent Model
Section titled “From Eloquent Model”$user = User::find(1);$dto = UserDTO::fromModel($user);To Eloquent Model
Section titled “To Eloquent Model”$dto = UserDTO::fromArray($data);$user = new User();$dto->toModel($user);$user->save();Update Existing Model
Section titled “Update Existing Model”$user = User::find(1);$dto = UserDTO::fromRequest($request);$dto->toModel($user);$user->save();Request Validation
Section titled “Request Validation”Validate and Create
Section titled “Validate and Create”try { $dto = UserDTO::validateAndCreate($request->all()); $user = User::create($dto->toArray());} catch (ValidationException $e) { return response()->json(['errors' => $e->errors()], 422);}Custom Validation Messages
Section titled “Custom Validation Messages”class UserDTO extends SimpleDTO{ public function __construct( #[Required(message: 'Email is required')] #[Email(message: 'Invalid email format')] public readonly string $email, ) {}}Laravel-Specific Attributes
Section titled “Laravel-Specific Attributes”WhenAuth
Section titled “WhenAuth”Show property only when user is authenticated:
class UserProfileDTO extends SimpleDTO{ public function __construct( public readonly string $name,
#[WhenAuth] public readonly ?string $email = null, ) {}}WhenGuest
Section titled “WhenGuest”Show property only when user is guest:
#[WhenGuest]public readonly ?string $registerPrompt = null;WhenCan
Section titled “WhenCan”Show property when user has permission:
#[WhenCan('edit-posts')]public readonly ?string $editUrl = null;WhenRole
Section titled “WhenRole”Show property when user has role:
#[WhenRole('admin')]public readonly ?array $adminPanel = null;
// Multiple roles (OR logic)#[WhenRole(['admin', 'moderator'])]public readonly ?array $moderationPanel = null;Artisan Commands
Section titled “Artisan Commands”Generate DTO
Section titled “Generate DTO”php artisan make:dto UserDTOCreates app/DTOs/UserDTO.php:
<?php
namespace App\DTOs;
use event4u\DataHelpers\SimpleDTO\SimpleDTO;
class UserDTO extends SimpleDTO{ public function __construct( public readonly string $name, public readonly string $email, ) {}}Generate TypeScript
Section titled “Generate TypeScript”php artisan dto:typescriptGenerates TypeScript interfaces from your DTOs.
See also: Artisan Commands - Complete guide to all available Artisan commands