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.
ilhamsyabani/laravel-volt-starter
thethunderturner/filament-latex
ghostcompiler/laravel-querybuilder
webrek/laravel-telescope-mongodb
anousss007/blatui
zatona-eg/zatona-eg-api
cocosmos/filament-sticky-save-bar
patrickbussmann/oauth2-apple
3brs/enterprise-security-bundle
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat