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

goatshark's avatar

Default Value for Nova Number Field

Hello. I'm wondering if there’s a way to give this a default value.

Number::make('Quantity')->min(1)->max(1000)->step(1)

I feel like I’m missing something in the docs.

0 likes
5 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Have you tried this:

Number::make('Quantity')->min(1)->max(1000)->step(1)->withMeta(['value' => 1]);
2 likes
goatshark's avatar

@Nakov Good call!! That seems to have worked. Thank you. I had tried playing with ->resolveUsing and ->displayUsing() to no avail. I went back to the Nova documentation regarding withMeta. Even after re-reading it, I still would not have thought that withMeta was going to get this done for me.

Thanks for the suggestion.

Nakov's avatar

@GOATSHARK - There is an ongoing discussion on github here. So one guy has mentioned that it might not work for a Number field if the default value you try to set is 0. Just keep that in mind. As I saw that your min value is 1, I thought that this should work.

goatshark's avatar

@Nakov I appreciate the heads up. I was wondering how it was going to perform in real life, which I'll be testing within the next couple of hours. If smoke or leprechauns come out of it, I'll be sure to update the thread. Thanks!

ohho's avatar

To preserve the original value, ?? is a better option:

            Number::make('Quantity', 'quantity')->withMeta([
                'quantity' => $this->quantity ?? 1,
            ]),

Please or to participate in this conversation.