spatie / laravel-data

Powerful data objects for Laravel
https://spatie.be/docs/laravel-data/
MIT License
1.31k stars 212 forks source link

TransformedDataCollectableResolver: Argument #1 ($data) must be of type BaseData, array given #878

Open alexanderkroneis opened 1 month ago

alexanderkroneis commented 1 month ago

✏️ Describe the bug I'm implementing a nested Data object. When I manipulate $adults from outside (BookingEngine, which is a Livewire component) it manipulates $travelers as well. Once I type something in another input field it shows this message:

Spatie\LaravelData\Resolvers\TransformedDataCollectableResolver::Spatie\LaravelData\Resolvers\{closure}(): Argument #1 ($data) must be of type Spatie\LaravelData\Contracts\BaseData, array given, called in /var/www/vendor/spatie/laravel-data/src/Resolvers/TransformedDataCollectableResolver.php on line 81

↪️ To Reproduce Provide us a pest test like this one which shows the problem:

BookingEngine.php

class BookingEngine extends Component
{
    public BookingEngineData $data;

    // ...

    public function decrease(string $property): void
    {
        $this->data->{$property}--;

        if ($this->data->{$property} < 0) {
            $this->data->{$property} = 0;
        }

        $this->data->travelers->pop();
    }

    public function increase(string $property): void
    {
        $this->data->{$property}++;

        $this->data->travelers->add(new BookingEngineTravelerData);
    }

BookingEngineData.php

class BookingEngineData extends Data implements Wireable
{
    use WireableData;

    /** @var Collection<int, BookingEngineTravelerData> */
    public Collection $travelers;

    // ...
}

BookingEngineTravelerData.php

class BookingEngineTravelerData extends Data implements Wireable
{
    use WireableData;

    public string $name;

    public int $age;

    public float $amount = 0.0;
}

https://github.com/user-attachments/assets/5ec88b92-08a4-4fe2-abf1-9ad1e628d47f

✅ Expected behavior No exception (see second show case in the video).

🖥️ Versions

Laravel: 11.26.0 Laravel Data: 4.10.1 PHP: 8.3

skylerkatz commented 6 days ago

@alexanderkroneis did you ever figure out why this is happening? I am running into the same error.

alexanderkroneis commented 6 days ago

Actually, no. I refactored the code and it disappeared.