Download the PHP package bssphp/laraveldto without Composer

On this page you can find all versions of the php package bssphp/laraveldto. 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 laraveldto

Laravel DTO

Latest Stable Version Total Downloads License GitHub Build Status

A strongly typed Data Transfer Object for Laravel without magic for PHP 8.0+

This package extends the functionality of bssphp/DTO to provide more narrow usecases for Laravel applications.

Laravel-DTO serves as an intermediate and reusable layer between request input & validation and model attribute population.

Contents

Installation

Usage

All data objects must extend the bssphp\laraveldto\AbstractModelData class.

Validation

When attaching the available validation rules and even built-in rules instances.

This will throw a Illuminate\Validation\ValidationException if any rule does not pass.

Populate Models

You can attach a model to any DTO using the #[ForModel(Model::class)] attribute. To associate DTO properties with Model attributes, you need to attach the #[ModelAttribute()] attribute to each property. If no parameter is passed to the #[ModelAttribute] attribute, DTO uses the property name itself.

Create DTO and store to model

Attributes saved in Person model

Note: You can also pass an existing model to the toModel() method.

Note: When passing no existing model to the toModel() method, default values declared in the DTO will be populated. If a model is passed as argument toModel($model) default values will not override existing model attributes.

Populate DTO from request input data

When attaching the #[RequestAttribute] attribute, DTO uses the property name itself.

The controller

Request input data

The PersonData DTO instance

Combined usage

Of course all those attributes start to make sense if used together. You can attach all attributes separately of make use of the #[ValidationRule] attributes.

Both properties in the following example behave exactly the same. Use as you prefer.

Request input data

The controller

Nested data

In some cases you also want to create realted models with a single HTTP call. In this case you can make use of the #[NestedModelData(NestedData::class)] which will populate the DTO property with n instances of the defined DTO.

Note that we will not attach an #[ModelAttribute] attribute to the $address DTO property since it should not be set to a model attribute.

All attributes attached to the nested DTO will just work as expected.

Request input data

The controller

Type Casting

Type casts will convert any given value to a specified type.

Built-in type casts

CastToDate

The #[CastToDate] attribute will respect your customly defined date class from Date::use(...). You can also specify a custom date class to be used by passing the date class name as single argument #[CastToDate(MyDateClass::class)].

Custom type casts

You can declare custom type cast attributes by simply implementing the CastInterface interface and attaching an attribute.

Best practices

Make sure to add a @method PHPDoc comment like shown below to allow IDE and static analyzer support when calling the toModel() method.

TODO

Testing

PHPUnit

PHPStan

Authors


All versions of laraveldto with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
ext-json Version *
bssphp/dto Version ^1.4
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 bssphp/laraveldto contains the following files

Loading the files please wait ....