In laravel I have used spatie/data-transfer-object to cast one of my column as
protected $casts = [
'details' => ProductDetails::class,
];
In my ProductDetails DTO I further want to cast two of my date fields
class ProductDetails extends CastableDataTransferObject
{
use HasDates;
public int $number
#[CastWith(ProductDateCaster::class)]
public int $start_date; //est DD/MM/YY
#[CastWith(ProductDateCaster::class)]
public int $end_date;
}
Here is my Caster
class ProductDateCaster implements Caster{
public function cast(mixed $value): string
{
$productDetails = new ProductDetails();
return $productDetails->parseDate($value)->format('d/m/Y');
}
}
I'm not doing it the right way, the caster is creating an object of DTO and is providing null to fields other than casted one. Can anyone please suggest the right way for using a caster on specific fields of DTO
Use the example blew as a starting point '?' is null safe for PHP8 always use that.
}