laralgato/request-object
最新稳定版本:1.1.0
Composer 安装命令:
composer require laralgato/request-object
包简介
Simple way to convert your request data to an object.
README 文档
README
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 :)
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-09