Skip to main content

Select Dropdown with/without belongsTo

Premium
4:39

Text Version of the Lesson

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, let's make a PHP Enum class with values:

php artisan make:enum Enums/ProductStatusEnum --string

app/Enums/ProductStatusEnum.php:

namespace App\Enums;
 
enum ProductStatusEnum: string
{
case IN_STOCK = 'In Stock';
case SOLD_OUT = 'Sold Out';
case COMING_SOON = 'Coming Soon';
}

Then, we will have a string field in the database with the default value from that Enum, in a new Migration:

Database Migration:

use App\Enums\ProductStatusEnum;
 
// ...
 
return new class extends Migration
{
public function up(): void
{
Schema::table('products', function (Blueprint $table) {
$table->string('status')
->default(ProductStatusEnum::IN_STOCK->value);
});
}
};

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...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

P
PBTraining ✓ Link copied!

Hi great tutorial,

I am coming across an issue that I hope someone in the community could help with lets say for example I have the following

Users Organisations Teams

And I now want to create a User and I want to have a drop down of organisations I can pick, thats rather easy I can do that, however the issue im facing is lets say for example, there are specific Teams in a Organisation, how can I populate a drop down with those Teams and then multi select those teams.

I have created something like this

Forms\Components\Section::make('Teams')->schema([
Forms\Components\CheckboxList::make('teams')
->options(fn(Forms\Get $get): Collection => $get('organisation_id') ?
Team::whereHas('organisations', function ($subquery) use ($get) {
$subquery->where('id', $get('organisation_id'));
})->pluck('name', 'id') :
collect([]))
]),

Which seems to work however on the edit if I select the dropdown again of oganisations I would get an error, Ive tried looking most places however as we know you are the man to come to :D

Thanks in advance to anyone that could assist

PK
Povilas Korop ✓ Link copied!

This question is quite specific and individual, so I can't answer without reproducing the project, playing around and debugging.

Have you tried to ask on the official Fliament discord?

If that didn't help, you could maybe invite me to your GitHub (username PovilasKorop) and I could try to play around, but my time is extremely limited if I want to put out the course soon.

N
Nerijus ✓ Link copied!

You say that you get an error but you don't say what error. Error message might say a lot. But yes, as Povilas said, try asking in the official Filament discord server.

JH
Josh Hartmann ✓ Link copied!

Wow that is so insanly easy compaired to some data tables libraries I have used. Love it! Will see how it goes handling many to many relations later I assume :)

Thanks for the awesome tutorials as always.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.