addBoundingBox(); } public function editProject(Project $project) { $this->showProjectModal = true; $this->formData = $project->toArray(); $this->formData['boundingBoxes'] = $project->boundingBoxes->toArray(); } /** * Create a new API token. * * @return void */ public function createProject() { $this->resetErrorBag(); Validator::make([ 'name' => $this->formData['name'], 'boundingBoxes' => $this->formData['boundingBoxes'], ], [ 'name' => ['required', 'string', 'max:255'], 'boundingBoxes' => ['required', 'array', 'min:1'], 'boundingBoxes.*.name' => ['required', 'string', 'max:255'], 'boundingBoxes.*.top_left_latitude' => ['required', 'string'], 'boundingBoxes.*.top_left_longitude' => ['required', 'string'], 'boundingBoxes.*.bottom_right_latitude' => ['required', 'string'], 'boundingBoxes.*.bottom_right_longitude' => ['required', 'string'], ])->validateWithBag('createProject'); // Define the unique identifier for the Project (for example, 'id') $uniqueIdentifier = ['id' =>$this->formData['id'] ?? null]; // Using updateOrCreate to either update an existing project or create a new one $project = Project::updateOrCreate($uniqueIdentifier, $this->formData); // Preparing the bounding boxes data for upsert // Ensure that each bounding box data array includes a 'project_id' field $boundingBoxesData = array_map(function ($boundingBox) use ($project) { $boundingBox['project_id'] = $project->id; unset($boundingBox['created_at']); unset($boundingBox['updated_at']); if (!array_key_exists('id', $boundingBox)) { $boundingBox['id'] = null; } return $boundingBox; }, $this->formData['boundingBoxes'] ?? []); // Using upsert to update or create bounding boxes // You need to specify the unique fields and the fields to be updated ProjectBoundingBox::upsert($boundingBoxesData, ['id', 'project_id'], ['name', 'top_left_latitude', 'top_left_longitude', 'bottom_right_latitude', 'bottom_right_longitude']); $this->resetFormData(); $this->showProjectModal = false; $this->dispatch('saved'); } public function addBoundingBox() { $this->formData['boundingBoxes'][] = [ 'name' => '', 'top_left_latitude' => '', 'top_left_longitude' => '', 'bottom_right_latitude' => '', 'bottom_right_longitude' => '', ]; } public function deleteBoundingBox($index) { if(array_key_exists('id', $this->formData['boundingBoxes'][$index])) { ProjectBoundingBox::whereId($this->formData['boundingBoxes'][$index]['id'])->delete(); } unset($this->formData['boundingBoxes'][$index]); $this->formData['boundingBoxes'] = array_values($this->formData['boundingBoxes']); $this->dispatch('saved'); } /** * Confirm that the given Project should be deleted. * * @param int $reportId * @return void */ public function confirmReportDeletion($reportId) { $this->confirmingReportDeletion = true; $this->reportIdBeingDeleted = $reportId; } /** * Delete the project * * @return void */ public function deleteProject() { Project::whereId($this->reportIdBeingDeleted)->first()->delete(); $this->resetFormData(); $this->confirmingReportDeletion = false; } /** * Get the current user of the application. * * @return mixed */ public function getProjectsProperty() { return Project::all(); } public function render() { return view('livewire.project-manager'); } private function resetFormData() { $this->formData = [ 'name' => '', 'boundingBoxes' => [], ]; $this->addBoundingBox(); } }