Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Smart Dto Bundle Laravel Package

dreadnip/smart-dto-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer:

composer require dreadnip/smart-dto-bundle

Publish the bundle configuration (if needed):

php artisan vendor:publish --provider="Dreadnip\SmartDtoBundle\SmartDtoServiceProvider"

First Use Case: Create a DTO class extending SmartDto trait (replacing the previous abstract class approach):

use Dreadnip\SmartDtoBundle\SmartDto;

class UserDto extends SmartDto
{
    // Automatically handles casting, validation, and hydration
}

Implementation Patterns

Trait-Based Inheritance

Replace abstract class inheritance with the new SmartDto trait:

// Old (deprecated)
class UserDto extends AbstractSmartDto { ... }

// New (recommended)
class UserDto extends SmartDto { ... }

Common Workflows

  1. Automatic Casting: Define $casts property for type conversion:

    protected $casts = [
        'is_active' => 'boolean',
        'created_at' => 'datetime'
    ];
    
  2. Validation: Use $rules for model-like validation:

    protected $rules = [
        'email' => 'required|email',
        'age' => 'integer|min:18'
    ];
    
  3. Hydration: Map request data automatically:

    $dto = UserDto::fromRequest($request);
    
  4. Service Integration: Bind DTOs in service providers:

    $this->app->bind(UserDto::class, function () {
        return new UserDto();
    });
    

Gotchas and Tips

Breaking Changes

  • Abstract Class Removal: The AbstractSmartDto class is now deprecated. Use SmartDto trait instead.
  • Trait Composition: Ensure your DTOs don’t conflict with other traits (e.g., Arrayable or Jsonable).

Debugging Tips

  • Validation Errors: Check $dto->errors() for validation failures.
  • Casting Issues: Use dd($dto->getOriginal()) to inspect raw data before casting.

Extension Points

  • Custom Casting: Override castAttribute() for custom logic:

    public function castAttribute($key, $value)
    {
        if ($key === 'custom_field') {
            return strtoupper($value);
        }
        return parent::castAttribute($key, $value);
    }
    
  • Dynamic Rules: Implement getRules() for dynamic validation:

    public function getRules()
    {
        return array_merge($this->rules, ['dynamic_field' => 'required_if:other_field,1']);
    }
    
  • Mass Assignment: Use $guarded to protect attributes:

    protected $guarded = ['password'];
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky