New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dealing with arrays of Value objects #4
Comments
Same here, I'd also like to do that. |
Hi @arrilot and @danielbehrendt, I can definitely see how that would be useful. It's currently only possible when the array of DTOs is nested within another DTO using the functionality provided by Spatie at https://github.com/spatie/data-transfer-object/#automatic-casting-of-nested-array-dtos. To have the array as the top-level, we'd need to create an object to represent the array, and thankfully Spatie seems to have us covered there too, with their To get that working within the casts provided by this package, I think we'd just need to create a |
@arrilot @danielbehrendt I've ported this package for POPOs using Symfony's serializer, which can support casting objects, dates and arrays. https://github.com/morrislaptop/laravel-castable-object I'd love to hear if this suits your needs! Basically it works like this namespace App\Models;
use App\Values\Address;
use Illuminate\Database\Eloquent\Model;
use Morrislaptop\Caster\Caster;
/**
* @property Address[] $addresses
*/
class User extends Model
{
protected $casts = [
'addresses' => Caster::class . ':' . Address::class . '[]',
];
} I haven't tried it yet, but it could also potentially work for DTO's as well by adding a @jessarcher would love your feedback on my package as well! |
Hello, what if we have a jsonb column addresses with
[ {...}. {...}, {...} ]
Is there any way to cast it to
[ Address(...), Address(...), Address(...), ]
?
The text was updated successfully, but these errors were encountered: