clear_previous) { $model->technicians()->detach(); } // Assign selected technicians foreach ($fields->technicians as $techId) { // Check if already assigned if (!$model->technicians->contains($techId)) { $model->technicians()->attach($techId, [ 'is_lead' => in_array($techId, $fields->lead_technicians) ? true : false ]); } } // Update status if needed if ($model->status === 'pending') { $model->status = 'in_progress'; $model->assigned_at = Carbon::now(); $model->started_at = Carbon::now(); // Update vehicle status $vehicle = $model->vehicle; $vehicle->status = 'in_repair'; $vehicle->save(); } $model->save(); } return Action::message('Technicians have been assigned successfully.'); } public function fields(NovaRequest $request): array { return [ BelongsToMany::make('Technicians') ->rules('required'), BelongsToMany::make('Lead Technicians', 'technicians', 'App\Nova\Technician') ->help('Select technicians who will lead this repair'), Boolean::make('Clear Previous Assignments', 'clear_previous') ->help('Remove all previously assigned technicians') ->default(false), ]; } }