Back to Course |
Filament Adminpanel for Booking.com API Project

Manage Geographical Objects

Now that we have countries and cities, in this lesson let's add geographical objects (like "Big Ben" or "Statue of Liberty") to the same Geography group.

geoobject form


First, create the resource and add it to its group. Also, we need to change the model, because the model is called GeoObject but I decided to call the Filament Resource with full name GeographicalObject.

php artisan make:filament-resource GeographicalObject

app/Filament/Resources/GeographicalObjectResource.php:

use App\Models\Geoobject;
use App\Models\GeographicalObject;
 
class GeographicalObjectResource extends Resource
{
protected static ?string $model = GeographicalObject::class;
protected static ?string $model = Geoobject::class;
 
protected static ?string $navigationIcon = 'heroicon-o-collection';
 
protected static ?string $navigationGroup = 'Geography';
// ...
}

geoobject navigation

Before adding the form and table, we need to add a city relationship to the Geoobject model. This is needed to use a select field for the cities and show the city in the table.

app/Models/Geoobject.php:

use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class Geoobject extends Model
{
use HasFactory;
 
protected $fillable = ['city_id', 'name', 'lat', 'long'];
 
public function city(): BelongsTo
{
return $this->belongsTo(City::class);
}
}

Now let's add form and table to the resource. In the form, we will reuse the LatitudeRule and LongitudeRule custom rules we made earlier.

app/Filament/Resources/GeographicalObjectResource.php:

use App\Rules\LatitudeRule;
use App\Rules\LongitudeRule;
 
class GeographicalObjectResource extends Resource
{
// ...
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->columnSpanFull(),
Forms\Components\TextInput::make('lat')
->required()
->rules([new LatitudeRule()]),
Forms\Components\TextInput::make('long')
->required()
->rules([new LongitudeRule()]),
Forms\Components\Select::make('city_id')
->relationship('city', 'name')
->preload()
->required()
->searchable()
->columnSpanFull(),
]);
}
 
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('lat'),
Tables\Columns\TextColumn::make('long'),
Tables\Columns\TextColumn::make('city.name'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}
// ...
}

The form and table should look similar to the ones below.

geoobject form

geoobject table