1

I have one field in a database table and its default value is true now when someone creates an entry from Laravel nova that time I want to pass true by default without showing that field in creating and update form... so can anyone help me how can I solve this.

Boolean::make('Is Active', 'is_active')->onlyOnIndex(),
Jigar
  • 3,055
  • 1
  • 32
  • 51

2 Answers2

8

This worked for me

    Boolean::make('Active','active')
        ->trueValue('1')
        ->falseValue('0')
        ->withMeta(['value' => $this->active ?? true]),

On create the value of the active column will be null/empty as it is a new "item" and knows nothing of the DB default value This will set the form active field to true. On update it will use the DB value.

dmgd
  • 425
  • 4
  • 15
  • 1
    Thanks for your answer but this will not work as $this->active will always be false if it's not checked .... – Jigar Aug 08 '19 at 16:28
4

You can set the default value on your model and nova should automaticly pick it up.

https://laravel.com/docs/5.8/eloquent#default-attribute-values

protected $attributes = [
    'is_active' => true,
];
Robbin Benard
  • 1,542
  • 11
  • 15
  • hey robbin I already set default value in database level but here when someone store or create a record from Laravel nova that time I just want to set a default value to true. – Jigar Aug 02 '19 at 13:36
  • and i am using that field so many places in normal laravel project but only in nova i want to pass default value – Jigar Aug 02 '19 at 13:37
  • Is this maybe something you are looking for https://stackoverflow.com/questions/52389551/laravel-nova-resource-extending-overriding-the-create-method – Robbin Benard Aug 02 '19 at 14:46
  • as I see there is no way to do so your answer will be helpful to achieve my task thanks. – Jigar Aug 02 '19 at 15:03