Download the PHP package novius/laravel-dto without Composer

On this page you can find all versions of the php package novius/laravel-dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-dto

Laravel DTO

A simple and extensible DTO (Data Transfer Objects) package for Laravel.

This package allows you to define structured data objects with built-in validation, default values, and automatic casting, while supporting both camelCase and snake_case naming conventions.

Installation

You can install the package via composer:

Artisan Command

You can easily generate a new DTO class using the following Artisan command:

By default, the class will be created in the app/Dtos directory.

Basic Usage

To create a DTO, simply extend the Novius\LaravelDto\Dto class and define your properties.

If you try to pass a property that is not defined in the class, an InvalidArgumentException will be thrown.

Features

Validation

Override the rules() method to define Laravel validation rules for your properties.

You can also customize validation messages and attributes by overriding the messages() and attributes() methods:

A ValidationException is thrown if the data does not comply with these rules during instantiation or modification via a setter.

Default Values

Override the defaults() method to define default values.

Casting

The package supports the same cast types as Laravel's Eloquent models. It also automatically infers the cast type from the native PHP type of your properties.

If you need more control, you can still override the casts() method. Explicit casts always take precedence over native type inference.

Supported types: int, float, string, bool, array, object, date, datetime, immutable_date, immutable_datetime, decimal:x, json, encrypted.

You can also cast properties to Backed Enums or Fluent objects:

You can also specify a custom format for date and datetime casts, which will be used when calling toArray():

Magic Getters, Setters and Fluent Interface

You can access your properties via magic methods. Setters automatically apply validation and casting.

The package also supports a fluent interface (methods with the same name as the property):

CamelCase / Snake_case Support

If your properties are defined in snake_case in your class, you can access them in camelCase seamlessly.

Array Conversion

The toArray() method returns all properties (public, protected, or private). It recursively handles nested DTOs. Dates are formatted according to their cast:

Property Mapping

You can map property names to different keys when converting the DTO to an array using the #[Map] attribute or the map() method.

The map() method takes precedence over the attribute.

Configuration via Attributes

In addition to methods, you can use PHP attributes to configure your properties directly.

Validation Rules

Use the #[Rules] attribute to define validation rules.

Default Values

Use the #[DefaultValue] attribute to define default values.

Custom Casting

Use the #[Cast] attribute to define a custom cast type.

Note: Methods (rules(), defaults(), casts()) always take precedence over attributes.

Excluding Properties from DTO Mechanisms

You can use the #[ExcludeFromDTO] attribute to completely exclude a property from all DTO mechanisms (constructor instantiation, magic getters/setters, fluent interface, validation, and toArray() output).

This is useful for properties that you want to keep in the class for internal use only, without them being part of the Data Transfer Object's public data flow.

Excluded properties are not present in the array representation:

Testing

The package uses Pest for testing.

Static Analysis

License

This project is licensed under the AGPL-3.0-or-later License.


All versions of laravel-dto with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/contracts Version ^11.0|^12.0|^13.0
illuminate/support Version ^11.0|^12.0|^13.0
illuminate/validation Version ^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package novius/laravel-dto contains the following files

Loading the files please wait ...