getSafe($request->sanitize()); // $data = [ // 'value' => sanitize_text_field($request->get('label')), // ]; $isCreated = LabelResource::createAndAttach($data); // LabelResource::addLabelToLabelRelationships($order, [ // 'labelable_id' => $orderId, // 'labelable_type' => Order::class, // 'new_label_ids' => $newLabelIds, // 'existing_label_ids' => $existingLabelIds // ]); if (is_wp_error($isCreated)) { return $isCreated; } return $this->response->sendSuccess($isCreated); } public function updateSelections(Request $request) { $data = $request->getSafe( [ 'bind_to_type' => 'sanitize_text_field', 'bind_to_id' => 'sanitize_text_field', 'selectedLabels.*' => 'sanitize_text_field' ] ); if (!empty($data['bind_to_type']) && !empty($data['bind_to_id'])) { if (!Str::contains($data['bind_to_type'], '\\')) { $modelClass = 'FluentCart\App\Models\\' . $data['bind_to_type']; } else { $modelClass = $data['bind_to_type']; } //Check If the model class exist if (class_exists($modelClass)) { /** * @var Model $modelInstance */ $modelInstance = new $modelClass(); $newLabelIds = Arr::get($data, 'selectedLabels', []); $model = $modelInstance->newQuery()->with('labels')->find($data['bind_to_id']); // Pluck and convert $existingLabelIds to a collection of strings $existingLabelIds = Collection::make($model['labels'])->pluck('label_id')->map(function ($value) { return (string)$value; }); $modelId = $model->{$modelInstance->getKeyName()}; $isAttached = LabelResource::addLabelToLabelRelationships($model, [ 'labelable_id' => $modelId, 'labelable_type' => $modelClass, 'new_label_ids' => $newLabelIds, 'existing_label_ids' => $existingLabelIds ]); if ($isAttached) { return $this->sendSuccess([ 'message' => __('Labels Updated Successfully', 'fluent-cart') ]); } return $this->sendError([ 'message' => __('Failed To Update Labels', 'fluent-cart') ]); } } } }