Jan 10, 2024
0
Level 8
Why I have to double parameters with wendelladriel/laravel-validated-dto in store request?
I make an API for storing data in the task_user table:
CREATE TABLE `task_user` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`task_id` bigint unsigned NOT NULL,
`user_id` bigint unsigned NOT NULL,
`team_lead` tinyint(1) NOT NULL DEFAULT '0',
`expires_at` date DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
using vsc Version: 1.85.1 and Thunder Client plugin.
I have a route
POST api/tasks/{task_id}/task-store-many-users/{user_id} ..................................................................... tasks.taskManyUsersStore › Api\TaskUserController@taskManyUsersStore
with 2 parameters, task_id and user_id and I fill sample data in next way :
I use wendelladriel/laravel-validated-dto 3.3 plugin, which has defined :
namespace App\DTO;
use DateTime;
use WendellAdriel\ValidatedDTO\Casting\BooleanCast;
use WendellAdriel\ValidatedDTO\Casting\CarbonCast;
use WendellAdriel\ValidatedDTO\Casting\IntegerCast;
use WendellAdriel\ValidatedDTO\Casting\StringCast;
use WendellAdriel\ValidatedDTO\SimpleDTO;
class TaskUserDTO extends SimpleDTO
{
public int $task_id;
public int $user_id;
public bool $team_lead;
public ?datetime $expires_at = null;
public ?datetime $created_at = null;
protected function defaults(): array
{
//
return [];
}
protected function casts(): array
{
return [
'task_id' => new IntegerCast(),
'user_id' => new IntegerCast(),
'team_lead' => new BooleanCast,
'expires_at' => new CarbonCast,
'created_at' => new CarbonCast
];
}
}
So using dto in controller :
public function taskManyUsersStore(Request $request, int $taskId, int $userId): JsonResponse {
try {
$data = TaskUserDTO::fromRequest($request);
I got error :
{
"message": "Cannot assign null to property App\DTO\TaskUserDTO::$task_id of type int",
"exception": "TypeError",
"file": "/mnt/_work_sdb8/wwwroot/lar/TasksWithSearch/vendor/wendelladriel/laravel-validated-dto/src/SimpleDTO.php",
"line": 237,
"trace": [
{
"file": "/mnt/_work_sdb8/wwwroot/lar/TasksWithSearch/vendor/wendelladriel/laravel-validated-dto/src/SimpleDTO.php",
"line": 156,
"function": "validatedData",
"class": "WendellAdriel\ValidatedDTO\SimpleDTO",
"type": "->"
To avoid this error, I need to add task_id and user_id parameters in Data Form - it seems to me that it is not valid to pass any parameter twice. Sure, the Thunder Client plugin is a tool, but in real work, any parameter must be passed twice. In which way can it be salved?
Please or to participate in this conversation.
