F
Filament17mo ago
Mdk

How to display and update pivot field in attach/edit form?

I've got a table, Places, that has a name and a description fields among other, and it's linked to an Itinerary table through a pivot, Itinerary_Place. This pivot table also has both name and description, so I can override the default ones when attaching a place to an itinerary. I can't find a way to display AND update the pivot name/description though, I can either display it by using make('pivot_name') but then it fails when it's time to save, or I can use make('name') and it'll update the correct field in the pivot table but it will always display the place's original name (same goes for the description) What am I missing?
2 Replies
techenby
techenby17mo ago
I recently did this for a project. I had a jobs table and a job_user table with a task on the job_user table. Here's what my relation manager looked like:
<?php

namespace App\Filament\Resources\JobResource\RelationManagers;

use App\Models\Job;
use Filament\Forms\Components\Select;
use Filament\Resources\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Table;
use Filament\Tables\Columns\TextColumn;

class UsersRelationManager extends RelationManager
{
protected static string $relationship = 'users';

protected static ?string $recordTitleAttribute = 'name';

public static function form(Form $form): Form
{
return $form
->schema([
Select::make('task')
->options([
Job::EXCAVATE => 'Locate and Excavate',
Job::BACKFILL => 'Backfill and Clean',
'other' => 'Other',
])
->required(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name')
->label('Employee'),
TextColumn::make('pivot.task')
->enum([
Job::EXCAVATE => 'Locate and Excavate',
Job::BACKFILL => 'Backfill and Clean',
'other' => 'Other',
])
->label('Assigned Task'),
])
->filters([
//
])
->actions([
//
])
->bulkActions([
//
]);
}
}
<?php

namespace App\Filament\Resources\JobResource\RelationManagers;

use App\Models\Job;
use Filament\Forms\Components\Select;
use Filament\Resources\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Table;
use Filament\Tables\Columns\TextColumn;

class UsersRelationManager extends RelationManager
{
protected static string $relationship = 'users';

protected static ?string $recordTitleAttribute = 'name';

public static function form(Form $form): Form
{
return $form
->schema([
Select::make('task')
->options([
Job::EXCAVATE => 'Locate and Excavate',
Job::BACKFILL => 'Backfill and Clean',
'other' => 'Other',
])
->required(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name')
->label('Employee'),
TextColumn::make('pivot.task')
->enum([
Job::EXCAVATE => 'Locate and Excavate',
Job::BACKFILL => 'Backfill and Clean',
'other' => 'Other',
])
->label('Assigned Task'),
])
->filters([
//
])
->actions([
//
])
->bulkActions([
//
]);
}
}
On the form I used task but on the table I used pivot.task
Mdk
MdkOP17mo ago
But what does it display in the form when you try to edit it? I'm using the same setup but when it comes to editing it shows the non-pivot value (even though it saves in the correct pivot table) Here's my actual code
Public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->maxLength(255)
->columnSpanFull(),
Forms\Components\MarkdownEditor::make('pivot.description')
->maxLength(65535)
->columnSpanFull(),
Forms\Components\DateTimePicker::make('time_from')->withoutSeconds()->required()
->default(function (RelationManager $livewire): array {
return $livewire->ownerRecord->places->last()->time_to;
}),
Forms\Components\DateTimePicker::make('time_to')->withoutSeconds()->required(),
]);
}
Public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->maxLength(255)
->columnSpanFull(),
Forms\Components\MarkdownEditor::make('pivot.description')
->maxLength(65535)
->columnSpanFull(),
Forms\Components\DateTimePicker::make('time_from')->withoutSeconds()->required()
->default(function (RelationManager $livewire): array {
return $livewire->ownerRecord->places->last()->time_to;
}),
Forms\Components\DateTimePicker::make('time_to')->withoutSeconds()->required(),
]);
}
i'm skipping the table part, which works perfectly as all it do is attaching
Want results from more Discord servers?
Add your server