inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.01k stars 224 forks source link

Providing empty object to the view #573

Closed kusiormarcin closed 4 months ago

kusiormarcin commented 7 months ago

Hi

Issue: Can't provide empty object to the view. It's automatically converted to array. When I use useForm() and provide a prop (data from PHP) to the function, it creates const for every property.

const form = useForm(props.items);

The issue here is that I have a complicated form with multiple objects inside.

Some objects are empty while creating the resource, this is a normal behavior, because there is no data. These objects are initialized as arrays, and then they accept properties for whatever reason. But they are presented as an array.

When the form is sent, data is visible as incorrect array and PHP is getting empty array.

JS:

Screenshot 2023-12-28 at 15 59 59

PHP:

Screenshot 2023-12-28 at 16 03 27

The only workaround I found is to provide a PHP array with any named index, example:

$object = ['o' => null];

Then it's correctly recognized as Object instead of array.

Any tricks like this doesn't work:

$object = (object)[];
$object = new \stdClass();

Is there any way to resolve this without workarounds?

driesvints commented 4 months ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.