The name cell on row 2 is empty
CSV writes id,name,email for an array whose second object has email and no name. TypeScript and the PHP DTO keep id and name from the first object.
The name column on row 2 is blank. This array gives name to the first object and email to the second, and the CSV writer still emits one header from every top-level key.
id,name,email
1,Ada,
2,,linus@example.com
Read 1,Ada, as id 1, name Ada, and an empty email. Read 2,,linus@example.com as id 2, an empty name, and that email address. The comma with nothing between it and the next comma is the empty cell.
Laravel does print the second object, so email survives there as a quoted key.
[
[
'id' => 1,
'name' => 'Ada',
],
[
'id' => 2,
'email' => 'linus@example.com',
],
]
The TypeScript interface and the PHP class are a different cut. With an array at the root, both are taken from the first object, {"id":1,"name":"Ada"}. Root lists id and name. email is not a field on that interface, and it is not a property on the class.
export interface Root {
id: number;
name: string;
}
<?php
declare(strict_types=1);
class DataTransferObject
{
public int $id;
public string $name;
public function __construct(array $data = [])
{
$this->id = $data['id'] ?? null;
$this->name = $data['name'] ?? null;
}
}
Those two assignments are the whole constructor. Parsing and the four conversions stay in the browser. The JSON converter does not send the paste to an API.
The sample with meta, and the questions beside it, are in the guide: https://kepttools.com/guides/json-to-typescript-php-dto-laravel
Comments
Join the discussionNo comments yet. Be the first to comment.

