1. Go to this page and download the library: Download romanzipp/laravel-dto 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/ */
romanzipp / laravel-dto example snippets
use App\Models\Person;
use App\Models\Project;
use Illuminate\Validation\Rules\Exists;
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\ForModel;
use romanzipp\LaravelDTO\Attributes\ValidationRule;
use romanzipp\LaravelDTO\Attributes\ValidationChildrenRule;
class PersonData extends AbstractModelData
{
#[ValidationRule([', ValidationChildrenRule(['string'], '*.device'), ValidationChildrenRule(['ipv4'], '*.ip')]
public array $logins;
}
use App\Models\Person;
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\ForModel;
use romanzipp\LaravelDTO\Attributes\ModelAttribute;
#[ForModel(Person::class)]
class PersonData extends AbstractModelData
{
#[ModelAttribute] // The `$name` DTO property will populate the `name` model attribute
public string $name;
#[ModelAttribute('current_age')] // The `$currentAge` DTO property will populate the `current_age` model attribute
public int $currentAge;
public string $language; // The `$language` DTO property will be ignored
}
$data = new PersonData([
'name' => 'John Doe',
'currentAge' => 25,
'language' => 'de',
]);
$person = $data->toModel()->save();
use App\Models\Person;
$person = $data->toModel($person)->save();
use App\Models\Person;
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\ForModel;
use romanzipp\LaravelDTO\Attributes\ModelAttribute;
use romanzipp\LaravelDTO\Attributes\RequestAttribute;
#[ForModel(Person::class)]
class PersonData extends AbstractModelData
{
#[RequestAttribute] // The `$name` DTO property will be populated by the `name` request attribute
public string $name;
#[RequestAttribute('my_age')] // The `$currentAge` DTO property will be populated by `my_age` request attribute
public int $currentAge;
public string $language; // The `$language` DTO property will not be populated
}
use App\Data\PersonData;
use Illuminate\Http\Request;
class TestController
{
public function store(Request $request)
{
$data = PersonData::fromRequest($request);
}
}
use App\Data\PersonData;
use Illuminate\Http\Request;
class TestController
{
public function index(Request $request)
{
$person = PersonData::fromRequest($request)->toModel()->save();
return $person->id;
}
}
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\ValidationChildrenRule;
class PersonData extends AbstractModelData
{
#[ValidationChildrenRule(['string', 'ipv4'], '*')];
public array $logins;
}
$data = new PersonData([
'logins' => [
'127.0.0.1',
'127.0.0.1'
]
]);
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\ValidationChildrenRule;
class PersonData extends AbstractModelData
{
#[ValidationChildrenRule(['string', 'ipv4'], '*.ip')];
public array $logins;
}
$data = new PersonData([
'logins' => [
['ip' => '127.0.0.1'],
['ip' => '127.0.0.1']
]
]);
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\ValidationChildrenRule;
class PersonData extends AbstractModelData
{
#[
ValidationChildrenRule(['string', 'ipv4'], '*.ip'),
ValidationChildrenRule(['string'], '*.device')
];
public array $logins;
}
$data = new PersonData([
'logins' => [
['ip' => '127.0.0.1', 'device' => 'iOS'],
['ip' => '127.0.0.1', 'device' => 'macOS']
]
]);
use App\Models\Person;
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\ForModel;
use romanzipp\LaravelDTO\Attributes\NestedModelData;
use romanzipp\LaravelDTO\Attributes\RequestAttribute;
use romanzipp\LaravelDTO\Attributes\ValidatedRequestModelAttribute;
use romanzipp\LaravelDTO\Attributes\ValidationRule;
#[ForModel(Person::class)]
class PersonData extends AbstractModelData
{
#[ValidatedRequestModelAttribute(['
use App\Models\Address;
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\ValidatedRequestModelAttribute;
#[ForModel(Address::class)]
class AddressData extends AbstractModelData
{
#[ValidatedRequestModelAttribute(['string'])]
public string $street;
#[ValidatedRequestModelAttribute(['nullable', 'int'])]
public ?int $apartment = null;
}
use App\Data\PersonData;
use Illuminate\Http\Request;
class TestController
{
public function index(Request $request)
{
$personData = PersonData::fromRequest($request);
$person = $personData->toModel()->save();
foreach ($personData->addresses as $addressData) {
// We assume the `Person` model has a has-many relation with the `Address` model
$person->addresses()->save(
$addressData->toModel()
);
}
return $person->id;
}
}
use Carbon\Carbon;
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\Casts\CastToDate;
class PersonData extends AbstractModelData
{
#[CastToDate]
public Carbon $date;
}
use Attribute;
use romanzipp\LaravelDTO\Attributes\Casts\CastInterface;
#[Attribute]
class MyCast implements CastInterface
{
public function castToType(mixed $value): mixed
{
return (string) $value;
}
}
use App\Models\Person;
use romanzipp\LaravelDTO\AbstractModelData;
use romanzipp\LaravelDTO\Attributes\ForModel;
use romanzipp\LaravelDTO\Attributes\ModelAttribute;
/**
* @method Person toModel()
*/
#[ForModel(Person::class)]
class PersonData extends AbstractModelData
{
#[ModelAttribute]
public string $name;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.