admin管理员组

文章数量:1392007

The learner is still learning filamentphp I have a resource with dependent select fields and they are working as expected. The relationship saves correctly. However I am trying to get the form records and strangely I don't get the checkbox values. What am I not doing right ? Find my code bellow

<?php

namespace App\Filament\Portal\Resources;
use Carbon\Carbon;
use Filament\Forms;
use Filament\Tables;
use App\Models\Subject;
use App\Models\ExamName;
use Filament\Forms\Form;
use App\Models\Signature;
use App\Models\ExamCenter;
use Filament\Tables\Table;
use Illuminate\Support\Str;
use App\Models\ExamNameCenter;
use Barryvdh\DomPDF\Facade\Pdf;
use Filament\Resources\Resource;
use App\Models\Users\Application;
use Illuminate\Support\HtmlString;
use App\Traits\ExamCenterBookedSeat;
use Filament\Support\Enums\MaxWidth;
use Filament\Forms\Components\Select;
use Illuminate\Support\Facades\Blade;
use App\Traits\CandidateSubjectAction;
use App\Traits\CheckIfSeatIsAvailable;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Fieldset;
use Filament\Tables\Actions\EditAction;
use Illuminate\Database\Eloquent\Model;
use Filament\Notifications\Notification;
use Illuminate\Database\Eloquent\Builder;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Actions\Action;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Portal\Resources\CandidateApplicationResource\Pages;
use App\Filament\Portal\Resources\CandidateApplicationResource\RelationManagers;
use App\Models\ExamSchedulePassword;

Resource Class

class CandidateApplicationResource extends Resource
{
    protected static ?string $model = Application::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
    protected static ?string $pluralModelLabel = 'Exam Applications';
    //protected static ?string $navigationLabel = 'Applications';

Form

    public static function form(Form $form): Form
    {


        //get the allowable number of paper
        return $form
            ->schema([
                Section::make()->schema([
                    Forms\Components\Placeholder::make('institution')->content(function($record){
                        return new HtmlString('<b>'.$record?->institution->name.'</b>');
                    })->columnSpanFull(),
                    Forms\Components\Placeholder::make('serial_number')
                                    ->content(function($record){
                                       session(['numberOfPapers'.auth()->user()->id=> $record?->VoucherCode?->revenueItem?->number_of_papers]);

                                        return new HtmlString('<b>'.$record?->serial_number.'</b>');
                                    }),
                    Forms\Components\Placeholder::make('code')
                                    ->content(function($record){
                                        return new HtmlString('<b>'.$record?->code.'</b>');
                                    }),
                    /**/
                ])->columns(3),
                Section::make()->schema([
                    Forms\Components\Placeholder::make('program')
                                ->content(function($record){
                                        return new HtmlString('<b>'.$record?->program?->name.'</b>');
                                    }),
                    Forms\Components\Placeholder::make('exam_name')
                                    ->content(function($record){
                                        return new HtmlString('<b>'.$record?->examName?->name.'</b>');
                                    }),

                    Forms\Components\Select::make('exam_center_id')->label('Exam Center')
                                ->live()
                                ->options(function($get){

                                return ExamCenter::without('name')
                                    ->whereIn('id',ExamNameCenter::where('exam_name_id',$get('exam_name_id'))
                                        ->pluck('exam_center_id'))
                                    ->pluck('name','id');
                                })
                                ->required()
                                ->preload()
                                ->searchable(),

                Forms\Components\Select::make('exam_name_center_id')->label('Examination Session')
                            ->searchable()
                            ->live()
                            ->required()
                            ->preload()->loadingMessage('Loading sessions...')
                            ->options(function($get,$record){

                                    $result = ExamNameCenter::where('exam_center_id', $get('exam_center_id'))
                                    ->where('exam_name_id', $get('exam_name_id'))
                                    ->whereColumn('booked_seats','<','max_capacity')
                                    ->where('is_active',true)
                                    ->selectRaw('CONCAT(name, " (", DATE_FORMAT(start, "%h:%i %p"), ")") as name, id')
                                        ->pluck('name', 'id')
                                        ->toArray();
                                    //merge the results
                                    if($record->examNameCenter){
                                        $examCenter = $record->examNameCenter;
                                        $start = Carbon::parse($examCenter->start);
                                    $currentResult = [$examCenter->id=>"{$examCenter->name} (". $start->format('h:i A').")"];
                                    }else{
                                        $currentResult = [];
                                    }
                                    $resultmerged = $result+$currentResult;
                                    // make array unique
                                    return array_unique($resultmerged);
                            })
                            ->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->name} (".Carbon::parse( $record->start)->format('h:i a').")"),

                ]),
                Section::make() ->visible(function($get){
                    return $get('exam_name_center_id') == Null? false: true;
                })->description(function(){

                    if(session()->has('numberOfPapers'.auth()->user()->id)){
                        $numberOfPapers =session()->get('numberOfPapers'.auth()->user()->id);
                        return new HtmlString('<em>You are required to select '.$numberOfPapers.' '.
                        Str::plural('subject',$numberOfPapers).' <b>only</b></em>');
                    }
                })->schema([
                    Forms\Components\CheckboxList::make('subject_id')
                    ->live()->rules([
                        function () {
                            return function (string $attribute, $value, $fail) {
                                  $numberOfPapers =session()->get('numberOfPapers'.auth()->user()->id);
                                if (count($value) <>  $numberOfPapers) {

                                    $fail('You must select '.$numberOfPapers.' '.
                                    Str::plural('subject',$numberOfPapers));
                                }
                            };
                        },
                    ])
                    ->dehydratedWhenHidden(true)
                         ->relationship('subjects','name',modifyQueryUsing:function($query,$record){

                                return $query->where('program_id', $record->program_id);
                            })
                            ->bulkToggleable(function(Forms\Components\CheckboxList $component){

                                if($numberOfPapers =session()->has('numberOfPapers'.auth()->user()->id)){
                                    $numberOfPapers =session()->get('numberOfPapers'.auth()->user()->id);
                                    if ( $numberOfPapers == count($component->getOptions()))  return true;
                                }

                                return false;
                            })
                        ->columns(2) ->required()
                        ->gridDirection('row'),
                ])

            ])->columns(1);
    }

Table of the resource

    public static function table(Table $table): Table
    {
        return $table
        ->recordUrl(' ')
         ->modifyQueryUsing(fn (Builder $query) => $query
                    ->with(['program','institution','examName','examNameCenter','examCenter','candidateSubjects','user']))
           /*  */->columns([
               
                Tables\Columns\TextColumn::make('code')
                    ->searchable(),
                Tables\Columns\TextColumn::make('program.shortname')->label('Program')
                    ->searchable()->sortable(),
                Tables\Columns\TextColumn::make('examName.name')->label('Exam Name')
                    ->searchable()->sortable(),
                Tables\Columns\TextColumn::make('examCenter.name')->label('Exam Center')
                    ->searchable()->sortable(),
               Tables\Columns\TextColumn::make('examNameCenter.name')->label('Session')
                ->description(function($record){
                    return Carbon::parse( $record->examNameCenter?->start)->format('h:i a');
                })->badge(),
              Tables\Columns\TextColumn::make('subjects.name')->wrap()
                    ->label('Subjects')
                    ->badge()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('institution.name')
                    ->label('Institution')
                    ->searchable()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),

            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                ->using(function (EditAction $action,Model $record, array $data): Model {
                            dd($data);
                            /*
                            
                            I only get exam_center_id and exam_name_center_id
                            I am not getting the subject_id
                            */
                           
                           
                        })->databaseTransaction(true)->successNotification(
                            Notification::make()
                                 ->success()
                                 ->title('Congratulations!')
                                 ->body('Your examination registration was successful.'),
                         ),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                ]),
            ]);
    }


I only get exam_center_id and exam_name_center_id I am not getting the subject_id

本文标签: laravelHow to get the select options before saving in filament resourceStack Overflow