FilamentF
Filament9mo ago
Batman

Select::make('category_id') VS. Select::make('Category')

Curious about the behavior differences.

I've seen tutorials that use Select::make('Category') and it works. I tried this method and it worked, until it didn't. It stopped working on creating, but continued working on update. I do understand documentation is Select::make('category_id') and that is what I am using, but I am just trying to get a better understanding. Does Filament (or Laravel) under the hood understand the relationship on update, but maybe not create?

Select::make('Category')
  ->relationship('category', 'name')
  ->placeholder('Select a category')
  ->required(),

VS.

Select::make('category_id')
  ->relationship('category', 'name')
  ->placeholder('Select a category')
  ->required(),

Relationship:
public function category(): BelongsTo
{
  return $this->belongsTo(Category::class);
}
Solution
It’s because ‘category’ exists as a relationship on the model so the field resolves it as such and tries to handle it appropriately. But if the relationship isn’t quite right the it falls apart so explicitly defining the relationship through the field modifiers make it work correctly.
Was this page helpful?