@props([ 'fields' => [], 'fieldName' => 'meta', 'min' => 1, 'max' => 5, 'multiple' => true, 'label' => 'Add Field', 'required' => false, 'existingData' => [], 'formKey' => null, // New prop for form identification 'title' => 'default', // New prop for meta title 'useTempMeta' => true, // Changed to true by default ]) @php // Generate unique ID for this component instance $componentId = 'dynamic-form-' . uniqid(); $formKey = $formKey ?? $componentId; // Parse existing data - extract status, order, and ID from Meta model columns, not from meta JSON $parsedData = []; if (!empty($existingData)) { foreach ($existingData as $index => $item) { if (is_object($item)) { // Extract meta JSON data (without status, order, and id) $metaData = isset($item->meta) ? (is_array($item->meta) ? $item->meta : json_decode($item->meta, true)) : []; // Ensure metaData is an array if (!is_array($metaData)) { $metaData = []; } // Add status, order, and ID from the model columns (separate from meta JSON) $parsedData[$index] = array_merge($metaData, [ 'status' => $item->status ?? 0, 'order' => $item->order ?? null, // Changed: Default order to null 'id' => $item->id ?? null, 'temp_id' => $item->temp_id ?? null // Include temp_id for temp meta ]); } elseif (is_array($item)) { $parsedData[$index] = $item; } } } // Fix: Determine the number of fields to render correctly $existingCount = count($parsedData); if ($min == 0 && $existingCount == 0) { $fieldsToRender = 0; // Render no fields initially when min=0 and no existing data } else { $fieldsToRender = max($min, $existingCount); } @endphp