In this lesson, let's review one of the most popular field types used in forms: dropdown selects. The data for the options may or may not come from another DB table as a relationship.
We will add two fields to our Products form:
- select to choose product status: "in stock", "sold out", "coming soon"
- select to choose product category: from another Category model
Simple Select Dropdown
Let's say that product may have one of the defined statuses, but we don't want to save them in a separate table.
Instead, we have an Enum field in the database:
Schema::table('products', function (Blueprint $table) { $table->enum('status', ['in stock', 'sold out', 'coming soon']) ->default('in stock');});
Let's make this field fillable in the Product
Model.
app/Models/Product.php:
class Product extends Model{ use HasFactory; protected $fillable = ['name', 'price', 'status'];}
And then, we can show the value in the table, just as a simple column:
app/Filament/Resources/ProductResource.php:
return $table ->columns([ Tables\Columns\TextColumn::make('name') ->sortable(), Tables\Columns\TextColumn::make('price') ->sortable() ->money('usd') ->getStateUsing(function (Product $record): float { return $record->price / 100; }), Tables\Columns\TextColumn::make('status'), ])
In the form, we add that field as...