Niblu
Niblu

Reputation: 11

Laravel nova optional value 0 on belongsTo field

I am learning Laravel Nova for a project and I have a simple nested categories table:

$table->bigIncrements('id')->unique();
$table->dropPrimary('id');
$table->string('name');
$table->bigInteger('parent');
$table->primary(['name', 'parent', 'id']);
$table->timestamps();

I have a model for Category with:

public function parent()
{
    return $this->belongsTo(Category::class, 'id', 'parent');
}

And I have a resource for Category in Nova with:

BelongsTo::make('Parent', 'parent', 'App\Nova\Category')

What I am trying to do is to have an option of selecting 0 (for top category with no parent) or choosing from existing categories.

Upvotes: 1

Views: 934

Answers (1)

Jacob Turner
Jacob Turner

Reputation: 31

Add ->nullable() to your Nova Field and it should work

BelongsTo::make('Parent', 'parent', 'App\Nova\Category')->nullable()

https://github.com/laravel/nova-issues/issues/89

Upvotes: 3

Related Questions