Assign record for EditAction
Hi there. I have a custom page where I'm building a standard HTML table and want to have an EditAction associated with each row in the table, but I cannot figure out how to assign the record to the action. I've tried many different ways with no success. I'm sure it's something simple, but I can't seem to figure out what I'm missing.
Currently, when I click the button I get an error saying the record is null. My current code looks like this
Relevant part of the blade file:
Relevant parts of the class:
@foreach ($this->open_enrollments as $open_enrollment)
<tr>
<td>
{{ ($this->assignStudent)(['enrollment' => $open_enrollment]) }}
</td>
</tr>
@endforeach
@foreach ($this->open_enrollments as $open_enrollment)
<tr>
<td>
{{ ($this->assignStudent)(['enrollment' => $open_enrollment]) }}
</td>
</tr>
@endforeach
namespace App\Filament\Pages;
...
use App\Models\Enrollment;
...
class MyClasses extends Page
{
public $open_enrollments;
#[Computed]
public function enrollments()
{
$user = Auth::user();
$user->loadMissing(['enrollments.course.nextEvent']);
return $user->enrollments;
}
public function assignStudent(): Action {
return EditAction::make('assignStudent')
->label('Assign Student')
->record(function (array $arguments) {
return $arguments['enrollment'] ?? null;
})
->schema([
Select::make('student_id')
->createOptionForm(fn (Schema $schema) => StudentForm::configure($schema, auth()->id()))
->relationship('student', 'id', function(Builder $query) {
return $query->where('user_id', auth()->id());
})
]);
}
public function mount() {
$this->open_enrollments = collect();
$this->enrollments->each(function (Enrollment $enrollment) {
if (!$enrollment->student_id) {
$this->open_enrollments->push($enrollment);
}
});
}
}
namespace App\Filament\Pages;
...
use App\Models\Enrollment;
...
class MyClasses extends Page
{
public $open_enrollments;
#[Computed]
public function enrollments()
{
$user = Auth::user();
$user->loadMissing(['enrollments.course.nextEvent']);
return $user->enrollments;
}
public function assignStudent(): Action {
return EditAction::make('assignStudent')
->label('Assign Student')
->record(function (array $arguments) {
return $arguments['enrollment'] ?? null;
})
->schema([
Select::make('student_id')
->createOptionForm(fn (Schema $schema) => StudentForm::configure($schema, auth()->id()))
->relationship('student', 'id', function(Builder $query) {
return $query->where('user_id', auth()->id());
})
]);
}
public function mount() {
$this->open_enrollments = collect();
$this->enrollments->each(function (Enrollment $enrollment) {
if (!$enrollment->student_id) {
$this->open_enrollments->push($enrollment);
}
});
}
}

1 Reply
I managed to get things working, but since I am assigning the record twice, once before form fill and again before saving the record, I feel like this can't be the right way of doing things.
@foreach ($this->open_enrollments as $open_enrollment)
<tr>
<td>
{{ ($this->assignStudent)(['enrollment_id' => $open_enrollment->id]) }}
</td>
</tr>
@endforeach
@foreach ($this->open_enrollments as $open_enrollment)
<tr>
<td>
{{ ($this->assignStudent)(['enrollment_id' => $open_enrollment->id]) }}
</td>
</tr>
@endforeach
use App\Filament\Resources\Students\Schemas\StudentForm;
use App\Models\Enrollment;
use App\Models\Student;
use Filament\Actions\Action;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Select;
use Filament\Pages\Page;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Builder;
class MyClasses extends Page
{
public function assignStudent(): Action
{
return EditAction::make('assignStudent')
->model(Enrollment::class)
->label('Assign Student')
->before(function(Action $action, array $arguments) {
$action->record(Enrollment::find($arguments['enrollment_id']));
})
->beforeFormFilled(function(Action $action, array $arguments) {
$action->record(Enrollment::find($arguments['enrollment_id']));
})
->schema([
Select::make('student_id')
->required()
->relationship('student', 'id', function(Builder $query) {
return $query->where('user_id', auth()->id());
})
->getOptionLabelFromRecordUsing(fn (Student $student) => $student->fullName)
->createOptionForm(fn (Schema $schema) => StudentForm::configure($schema, auth()->id()))
->createOptionUsing(function (array $data): int {
return auth()->user()->students()->create($data)->getKey();
}),
]);
}
}
use App\Filament\Resources\Students\Schemas\StudentForm;
use App\Models\Enrollment;
use App\Models\Student;
use Filament\Actions\Action;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Select;
use Filament\Pages\Page;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Builder;
class MyClasses extends Page
{
public function assignStudent(): Action
{
return EditAction::make('assignStudent')
->model(Enrollment::class)
->label('Assign Student')
->before(function(Action $action, array $arguments) {
$action->record(Enrollment::find($arguments['enrollment_id']));
})
->beforeFormFilled(function(Action $action, array $arguments) {
$action->record(Enrollment::find($arguments['enrollment_id']));
})
->schema([
Select::make('student_id')
->required()
->relationship('student', 'id', function(Builder $query) {
return $query->where('user_id', auth()->id());
})
->getOptionLabelFromRecordUsing(fn (Student $student) => $student->fullName)
->createOptionForm(fn (Schema $schema) => StudentForm::configure($schema, auth()->id()))
->createOptionUsing(function (array $data): int {
return auth()->user()->students()->create($data)->getKey();
}),
]);
}
}