laralgato/request-object

Simple way to convert your request data to an object.

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/laralgato/request-object

1.1.0 2025-07-29 18:35 UTC

This package is not auto-updated.

Last update: 2025-12-16 20:32:30 UTC


README

Latest Stable Version License

Simple way to convert your request data to an object.

Installation

Install via Composer:

composer require laralgato/request-object

Usage

After installing the package, you can simply add a trait and an attribute to your Request. The Attribute accepts the object that you want as a result of the toObject method.

use Laragato\RequestObject\Attributes\FormObject;
use Laragato\RequestObject\Traits\useObjectRequest;

#[FormObject(MyObject::class)]
class TestRequest extends FormRequest
{
    use useObjectRequest;
    ...
}

Creating the object with constructor :

In your controller you can simply use:

class TestController extends Controller
{
    public function __invoke(TestRequest $request)
    {
        $request->toObject();
    }
}

This line will perform the validation and return the object giving in the request. This is perfect for creating DTOs.

Creating Models object

If you want the result to be a Model or an object that requires the properties to be set one by one, you can use the toModel method:

class TestController extends Controller
{
    public function __invoke(TestRequest $request)
    {
        $request->toModel();
    }
}

This way you can simply call ->save() on your model

$model = $request->toModel();
$model->save();

In case you want to update the model with ->save(), you can add update: true to the Attribute. That will return copy of the model passed in the request with updated properties.

#[FormObject(MyObject::class, update: true)]

Then simply do

$model = $request->toModel();
$model->save();

To update the model.

Enjoy :)