1. Go to this page and download the library: Download qbejs/laravel-dto-mapper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
qbejs / laravel-dto-mapper example snippets
namespace App\DTOs;
use LaravelDtoMapper\Contracts\MappableDTO;
class CreateUserDTO implements MappableDTO
{
public string $name;
public string $email;
public int $age;
public function rules(): array
{
return [
'name' => '' => 'You must be at least :min years old.',
];
}
public function attributes(): array
{
return [
'name' => 'full name',
'email' => 'email address',
'age' => 'age',
];
}
}
namespace App\Http\Controllers;
use App\DTOs\CreateUserDTO;
use LaravelDtoMapper\Attributes\MapRequestPayload;
use Illuminate\Http\JsonResponse;
class UserController extends Controller
{
public function store(
#[MapRequestPayload] CreateUserDTO $dto
): JsonResponse {
$user = User::create([
'name' => $dto->name,
'email' => $dto->email,
'age' => $dto->age,
]);
return response()->json($user, 201);
}
}
public function store(
#[MapRequestPayload] CreateUserDTO $dto
): JsonResponse {
// $dto contains validated data from request body
}
public function index(
#[MapQueryString] UserFilterDTO $filters
): JsonResponse {
// $filters contains parameters from ?search=...&page=...
}
use Illuminate\Http\UploadedFile;
class CreatePostDTO implements MappableDTO
{
public string $title;
public ?UploadedFile $thumbnail;
public function rules(): array
{
return [
'title' => '
class CreatePostDTO implements MappableDTO
{
public string $title;
public array $attachments; // array of UploadedFile
public function rules(): array
{
return [
'title' => ': array { return []; }
}
public function store(
#[MapRequestPayload] CreatePostDTO $dto
): JsonResponse {
$post = Post::create(['title' => $dto->title]);
if ($dto->thumbnail) {
$path = $dto->thumbnail->store('thumbnails');
$post->update(['thumbnail' => $path]);
}
foreach ($dto->attachments as $file) {
$post->attachments()->create([
'path' => $file->store('attachments'),
]);
}
return response()->json($post, 201);
}
class BulkCreateUsersDTO implements MappableDTO
{
public array $users;
public function rules(): array
{
return [
'users' => '|min:18',
];
}
public function messages(): array { return []; }
public function attributes(): array { return []; }
}
// Usage
public function bulkStore(
#[MapRequestPayload] BulkCreateUsersDTO $dto
): JsonResponse {
foreach ($dto->users as $userData) {
User::create($userData);
}
return response()->json([
'created' => count($dto->users)
], 201);
}
// ✅ Good
public string $name;
public int $age;
public ?string $phone;
// ❌ Bad
public $name;
public $age;
// ❌ Wrong - will cause errors
class CreateUserDTO implements MappableDTO
{
public function __construct(
public string $name // DON'T DO THIS!
) {}
}
// ✅ Correct - only public properties
class CreateUserDTO implements MappableDTO
{
public string $name; // Just the property
}
// Request: ?deviceId=123 (lowercase 'd' in Id)
public string $deviceId; // Must match exactly!
// NOT: ?deviceID=123
// NOT: public string $deviceID;