Implementing Car Selection Logic in without using Session

I spent the entire day attempting to implement this logic without success. I have two models: 'CAR' and 'car_tenancy.' I only add car names to the 'car' resource. Currently, when I'm working on 'car_tenancy,' I want to ensure that if I select a car and return to create another entry, I shouldn't see the previously selected car. I've implemented the logic using 'session,' but I know it's not suitable for production. Could someone suggest an alternative? Here's a look at my code:
                        Forms\Components\Select::make('car_id')
                            ->options(function () {
                                // Retrieve selected car IDs from the session
                                $selectedCarIds = Session::get('selectedCarIds', []);
                                
                                // Filter out cars that have already been selected
                                $availableCars = Car::whereNotIn('id', $selectedCarIds)
                                    ->pluck('name', 'id')
                                    ->all();
                        
                                return $availableCars;
                            })
                            ->reactive()
                            ->required()
                            ->searchable()
                            ->preload()
                            ->required()
                            ->afterStateUpdated(function ( $set, $state) {
                                // Retrieve and update the selected car IDs in the session
                                $selectedCarIds = Session::get('selectedCarIds', []);
                                $selectedCarIds[] = $state;
                                Session::put('selectedCarIds', $selectedCarIds);
                        
                                $car = Car::find($state);
                                if ($car) {
                                }
                            }),
Solution
Yes Solved
  Forms\Components\Select::make('car_id')
                        ->options(function () {
                            $allocatedCars = CarTenancy::pluck('car_id')->toArray();

                            // Fetch available cars that are not allocated to any driver
                            $availableCars = Car::whereNotIn('id', $allocatedCars)->get();

                            return $availableCars->pluck('name', 'id')->toArray();
                        })
                        ->reactive()
                        ->searchable()
                        ->preload()
                        ->required()
Was this page helpful?