Dec 17, 2024
0
Level 2
Filament Custom Page not showing repeater data
I have created a custom page and in that repeater not showing the hasMany relation data.
<?php
namespace App\Filament\Resources\ProjectResource\Pages;
use App\Enums\ProjectStatus;
use App\Filament\Resources\ProjectResource;
use App\Models\Item;
use Filament\Actions\Action;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
use Filament\Resources\Pages\Page;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
class IssueProject extends Page implements HasForms
{
use InteractsWithRecord, InteractsWithForms;
public ?array $data = [];
protected static string $resource = ProjectResource::class;
protected ?string $heading = "Issue / Return Project Items";
protected static string $view = 'filament.resources.project-resource.pages.issue-return-project';
public function mount(int|string $record)
{
$this->record = $this->resolveRecord($record);
$this->record->load('projectItems');
if ($this->record->status !== ProjectStatus::Approved) {
// Redirect to the project list page
Notification::make()
->title('Project not Approved')
->danger()
->body('Please approve the project')
->send();
return Redirect::route('filament.project.resources.projects.index');
}
$this->data = $this->record->projectItems->toArray();
$this->form->fill();
}
public function form(Form $form): Form
{
return $form
->schema([
Section::make('Project Items')
->description('')
->schema([
Repeater::make('projectItems')
->relationship('projectItems')
->minItems(1)
->defaultItems(1)
->validationMessages([
'required' => 'You must add at least one component to the project.',
'min' => 'A project must have at least one component.',
])
->schema([
Grid::make(12)
->schema([
Select::make('item_id')
->label('Component Name')
->required()
->searchable()
->reactive()
->options(Item::select(DB::raw("CONCAT(name, ' - ', specification) AS display_name"), 'id')
->pluck('display_name', 'id'))
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->columnSpan(8),
TextInput::make('quantity')
->label('Requested')
->numeric()
->disabled()
->dehydrated(false)
->columnSpan(2),
TextInput::make('issued')
->label('Issued')
->numeric()
->required()
->columnSpan(2),
]),
])
])
])
->model($this->record);
}
protected function getFormActions(): array
{
return [
Action::make('save')
->label('Issue')
->color('primary')
->submit('save'),
];
}
public function save(): void
{
try {
$data = $this->form->getState();
dd($data);
} catch (Halt $exception) {
return;
}
}
}
and the view is
<x-filament-panels::page>
<div class="space-y-4">
<div class="p-4 bg-white rounded-lg shadow-lg">
<div class="flex">
<div class="flex-1 p-2">
<p class="text-gray-800"><span class="font-bold">Project Title:</span> {{$record->title ?? ''}}</p>
</div>
</div>
<div class="flex">
<div class="flex-1 p-2">
<p class="text-gray-800"><span class="font-bold">Level:</span> {{$record->level ?? ''}}</p>
</div>
<div class="flex-1 p-2">
<p class="text-gray-800"><span class="font-bold">Section:</span> {{$record->section ?? ''}}</p>
</div>
</div>
<div class="flex">
<div class="flex-1 p-2">
<p class="text-gray-800"><span class="font-bold">Teacher:</span> {{$record->teacher->name ?? ''}}</p>
</div>
</div>
</div>
<div class="p-4 bg-white rounded-lg shadow-lg">
<div class="flex">
<div class="flex-1 p-2">
<p class="text-gray-800"><span class="font-bold">Student Details</span></p>
</div>
</div>
@foreach($record->projectStudents as $projectStudent)
<div class="flex">
<div class="flex-1 p-2">
<p class="text-gray-800"><span class="font-bold">{{$loop->index +1 }}.</span> {{$projectStudent->student->student_info ?? ''}}</p>
</div>
</div>
@endforeach
</div>
<x-filament-panels::form wire:submit="save">
{{ $this->form }}
<x-filament-panels::form.actions
:actions="$this->getFormActions()"
/>
</x-filament-panels::form>
</div>
</x-filament-panels::page>
can you please help me how to resolve it.
Please or to participate in this conversation.