spatie / laravel-data

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

Livewire Synths not working in data object contains an array #836

Open Glennmen opened 1 month ago

Glennmen commented 1 month ago

✏️ Describe the bug When using the data object in Livewire wire:model.live="startupRule.daysActive" it produces an exception on model update. We are using it in a input checkbox.

↪️ To Reproduce

class PowerscheduleRuleData extends Data
{
    public ?array $daysActive = [];
}
2024-08-09 10:58:25] local.ERROR: Spatie\LaravelData\Support\Livewire\LivewireDataSynth::get(): Return value must be of type Spatie\LaravelData\Contracts\BaseData, array returned {"userId":"4","exception":"[object] (TypeError(code: 0): Spatie\\LaravelData\\Support\\Livewire\\LivewireDataSynth::get(): Return value must be of type Spatie\\LaravelData\\Contracts\\BaseData, array returned at /var/www/html/vendor/spatie/laravel-data/src/Support/Livewire/LivewireDataSynth.php:35)

I was able to fix it by editingthe get(&$target, $key) in LivewireDataSynth and by removing the return value BaseData typecast:

public function get(&$target, $key)
{
    return $target->{$key};
}

But not sure if that is the correct solution.

✅ Expected behavior Laravel data to work with Livewire in two way binding.

🖥️ Versions

Laravel: 11.16.0 Laravel Data: 4.7.2 PHP: 8.2 Livewire: 3.5.4

rubenvanassche commented 1 month ago

Can you provide me a demo repository showing this issue?

Glennmen commented 3 weeks ago

Sorry for the delay, I have been very busy. I will try to setup something this week.

Glennmen commented 3 weeks ago

@rubenvanassche Here you can see the issue that we where having: https://github.com/Glennmen/DemoLaravelDataBug

Very basis setup, can be run with artisan serve and then on the homepage you can see the error when changing the checkboxes.