Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aligajani's avatar

How can I have a Select in Nova that lets me store JSON in the database?

Sorry for the mouthful question, but in my signup page, I have something like this below. When a user selects any of the item from the options below, it gets stored in my database as a JSON value so say I select the 3rd option, it will show up as { name: 'Linkedin' } in my database.

I can technically have more keys inside { name: 'Linkedin' } and it will store that as well, while keeping the name for visibility purposes on the select itself. Is there anything like this possible in Nova, by default?

<SelectInput
                    v-model="form.reference"
                    rules="required"
                    label="How did you hear about us?"
                    :data="[
                        { name: 'Google' },
                        { name: 'Press' },
                        { name: 'Linkedin' },
                        { name: 'Facebook' },
                        { name: 'Twitter' },
                        { name: 'Advert' },
                        { name: 'Paradigm' },
                        { name: 'Referral' },
                        { name: 'Shares Magazine' },
                        { name: 'British Investor Summit' }
                    ]"
                />
0 likes
12 replies
piljac1's avatar

If I correctly understand what you're trying to accomplish, you could do something along those lines, but you're not stating if your JSON strings are dynamic (coming from database and can vary) or hardcoded. We also have no idea about what your database structure looks like, so it's kinda hard to help you there. The following example takes into account that your values are hard coded.

public function fields(Request $request)
{
    return [
        Select::make('Reference')->options([
            "{ name: 'Google' }" => "Google",
            "{ name: 'Press' }" => "Press",
            "{ name: 'Linkedin' }" => "Linkedin",
            "{ name: 'Facebook' }" => "Facebook",
            "{ name: 'Twitter' }" => "Twitter",
            "{ name: 'Advert' }" => "Advert",
            "{ name: 'Paradigm' }" => "Paradigm",
            "{ name: 'Referral' }" => "Referral",
            "{ name: 'Shares Magazine' }" => "Shares Magazine",
            "{ name: 'British Investor Summit' }" => "British Investor Summit"
        ])->displayUsingLabels();
    ];
}

Once again, if you want something dynamic, tell us more about your database structure and what behavior you expect.

1 like
aligajani's avatar

Most of my selects are static, but some are dynamic, like the country selector.

@piljac1 by the way, this is storing it in my database as "{ name: 'Press' }" , correct would be {"name": "Press"}

piljac1's avatar

That's my bad. What about if you format the keys correctly, does it work ?

aligajani's avatar

@piljac1 Nope. It adds slashes which I don’t want. I tried escaping slashes but even then same. I tried that it with json encode.

piljac1's avatar

What if you format your string in a proper JSON string within an observer matching your model ? That's the suggested Nova way to add an additional processing layer before actually interacting with the database itself.

You could use some kind of HTML legal markdown logic and interpret it afterwards (convert it to actual JSON)

aligajani's avatar

@piljac1 good one. I already do have model events. Not sure you mean specific to nova ? Could get messy. Any other ideas ?

piljac1's avatar

I don't know what you mean by messy because it is the suggested way:

Therefore, it is easy to listen for model events triggered by Nova and react to them. The easiest approach is to simply attach a model observer to a model

You can also set an observer to only be active within Nova (see the doc)

1 like
aligajani's avatar

@piljac1 thank you. I already do have model events for creating and saved so I guess nova is already triggering those. Except from what I understand you are suggesting stacking up a nova specific one ?

aligajani's avatar

@piljac1 Also, wont having 2 saving model events listen to a change conflict or collide?

piljac1's avatar

For sure it would need to be a Nova specific one to avoid that processing on the front-end, but will the two observers collide ? Well not on the front-end since you're registering one of the two in Nova's service provider only. But about Nova itself, I'd need to test it, because I never had that situation happen. As a side question, is your already in place observer required to run in Nova also ?

aligajani's avatar

@piljac1 So I implemented a Nova::serving and put in a UserObserver inside it. So far it is not colliding, as I am running a piece of code specific to Nova only. Do you know the sequence these run, that is, which event gets fired first? Nova observer event or the one setup in my dispatchEvents within the model?

piljac1's avatar

I don't know the sequence since I never used the $dispatchEvents way. The easiest way would be to call the dd function in both to determine in which order they're executed.

Please or to participate in this conversation.