kirkbushell / eloquence

A drop-in library for certain database functionality in Laravel, that allows for extra features that may never make it into the main project.
MIT License
537 stars 60 forks source link

Support eloquent casts when using Camelcasing #100

Closed mikebronner closed 5 months ago

mikebronner commented 2 years ago

When trying to access data in json attributes in models, Livewire is unable to access the snake-case format, which makes it problematic if you are in a transition period between moving from snake_case to camelCase.

I haven't dug into this yet to see how Livewire accesses model properties, but I'll try and give it a go and submit a PR if I can.

kirkbushell commented 2 years ago

Thanks, Mike. It shouldn't be an issue. Accessing attributes on Eloquence models converts all calls to attributes to snake_case when it tries to access an attribute. It doesn't maintain its own internal casing, so it's a weird bug. If you can find out more info, that'd be great (also a big livewire fan).

mikebronner commented 2 years ago

A bit more context:

Livewire validations:

    protected function rules(): array
    {
        return [
            "paymentProcessGroup.processor_settings.authorize-dot-net-login-id" => "string|nullable",
        ];
    }

For it to work, it must be written in camel case:

    protected function rules(): array
    {
        return [
            "paymentProcessGroup.processorSettings.authorize-dot-net-login-id" => "string|nullable",
        ];
    }

Here's a snippet of the model (it inherits the camel-casing trat from a parent model)

class PaymentProcessGroup
{
    protected $casts = [
        "processor_settings" => "array",
    ];
}

Maybe this will help ring a bell?

mikebronner commented 2 years ago

@kirkbushell Diving into Livewire's validation code, it appears they convert everything that gets validated to arrays by calling toArray() on models and collections. The CamelCase trait exposes only the camel-cased properties. Would it be a bad thing to expose both the snake-cased as well as the camel-cased properties via toArray() or would that cause issues?

I mean, it would probably mess with anyone trying to iterate over the array, so not quite sure what the best solution is here? What do you think?

kirkbushell commented 2 years ago

@mikebronner Have you tried doing the casting as a camelCase value instead?

The point of the camelCase trait is to use camelCasing wherever you're working with model fields.

mikebronner commented 2 years ago

I didn't ... do you write all your fillable, casts, etc. in camel-case? I didn't realize that the camel casing trait would allow for that.

kirkbushell commented 2 years ago

I didn't ... do you write all your fillable, casts, etc. in camel-case? I didn't realize that the camel casing trait would allow for that.

You shouldn't have to, the point of the library is to convert to-from snake_case, as this is the default mode when reading from the database. I was asking in case maybe that resolved it :)

More than likely I need to add better support for casts.

kirkbushell commented 10 months ago

@mikebronner right, gonna properly support casts in 11.0.

kirkbushell commented 5 months ago

@mikebronner should now be fully supported in v11.