Skip to content
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

FlexibleCast returning a string #311

Closed
dinandmentink opened this issue Nov 27, 2021 · 6 comments · May be fixed by #409
Closed

FlexibleCast returning a string #311

dinandmentink opened this issue Nov 27, 2021 · 6 comments · May be fixed by #409

Comments

@dinandmentink
Copy link
Contributor

dinandmentink commented Nov 27, 2021

I think I may be missing something obvious, but can't figure it out.

I have an Association model, to which I have added a CTA layout:

Flexible::make(trans("admin.ctas"), "ctas")
    ->button(trans("admin.cta"))
    ->addLayout(trans("admin.cta"), 'cta', [
        Text::make(trans("admin.title"), "title"),
        Text::make(trans("admin.link"), "link"),
        Select::make(trans("admin.color"), "color")
            ->options([
                "primary" => trans("admin.primary"),
                "secondary" => trans("admin.secondary"),
                "light" => trans("admin.light"),
                "dark" => trans("admin.dark"),
                "white" => trans("admin.white"),
            ]),
    ]),

In my Association model I use the FlexibleCast:

class Association extends Model
{
    protected $casts = [
        "ctas" => FlexibleCast::class,
    ];
}

Now, after saving some cta's the database holds this value in a json column, everything working as I would expect:

[{"key": "gZ16OPeItKGroBYo", "layout": "cta", "attributes": {"link": "/pages/contact", "color": "secondary", "title": "Contact"}}]

However, the cast doesn't seem to work. Because Association::find(2)->ctas returns to me the following String(!):

 "[{"key": "gZ16OPeItKGroBYo", "layout": "cta", "attributes": {"link": "/pages/contact", "color": "secondary", "title": "Contact"}}]"

It doesn't seem to be actually casting ctas to a collection of objects. Am I missing something? json_decode, or using `"json" as cast works as I would expect.

@toonvandenbos
Copy link
Member

Hi @dinandmentink, did you try using JSON casting just to see if all casts are not working or just the FlexibleCast ? Did you use Whitecube\NovaFlexibleContent\Value\FlexibleCast; ?

@dinandmentink
Copy link
Contributor Author

Thanks for the response. Yes, json casting works as I would expect:

protected $casts = [
        "ctas" => "json",
    ];
>>> App\Models\Association::first()->ctas
=> [
     [
       "key" => "nlxDnMG2P8kGt73Z",
       "layout" => "cta",
       "attributes" => [
         "link" => "/pages/contact",
         "color" => "primary",
         "title" => "Contact",
       ],
     ],
   ]

Also, yes confirm I am using Whitecube\NovaFlexibleContent\Value\FlexibleCast;:

protected $casts = [
    "ctas" => \Whitecube\NovaFlexibleContent\Value\FlexibleCast::class,
];
>>> App\Models\Association::first()->ctas
=> "[{"key": "nlxDnMG2P8kGt73Z", "layout": "cta", "attributes": {"link": "/pages/contact", "color": "primary", "title": "Contact"}}]"

@toonvandenbos
Copy link
Member

Inside the HasFlexible trait (which we're using in FlexibleCast) we're returning the raw value (string) when in the following context :

if(app()->getProvider(NovaServiceProvider::class) && !app()->runningInConsole() && !app()->environment('testing')) {
    return $value;
}

In other words, if you're testing inside Nova itself it won't return the casted value (because the field will take care of it by itself). Is that your case ?

@dinandmentink
Copy link
Contributor Author

dinandmentink commented Nov 29, 2021

Ah, yes, thanks, this is the case. This helps a lot, thanks.

I ran into this issue because I first tried "what kind of return do I get in tinker" before writing tests or checking the view. Now that I know I'm getting a different result in the console there's no issue anymore.

Edit: Maybe it would be a good idea to add this to the documentation. Is there a github repo for the documentation that I can write a pr against?

@ramunasartme
Copy link

I have the same issue. How to solve?

@TijmenWierenga
Copy link

@toonvandenbos I'm running into issues with casting as well due to the raw value being used in all contexts.

I'm in the situation where I have Nova installed in my application which is also exposing an API. This means the NovaServiceProvider is always there so my API will never cast into layouts.

Am I doing something wrong or is this by design?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants