@php $field['type'] = 'relationship.morphTo_select_ajax'; $optionsForModels = []; foreach ($field['morphOptions'] as $model => $options) { if (!isset($options['data_source'])) { continue; } if (!is_a($model, 'Illuminate\Database\Eloquent\Model', true)) { $model = $field['morphMap'][$model]; } $modelInstance = new $model(); $optionsForModels[$model]['attribute'] = $options['attribute'] ?? $modelInstance->identifiableAttribute(); $optionsForModels[$model]['minimumInputLength'] = $options['minimum_input_length'] ?? 0; $optionsForModels[$model]['ajax']['url'] = $options['data_source']; $optionsForModels[$model]['ajax']['method'] = $options['method'] ?? 'POST'; $optionsForModels[$model]['placeholder'] = $options['placeholder'] ?? $field['placeholder']; $optionsForModels[$model]['connectedKey'] = $modelInstance->getKeyName(); } $currentValue = old_empty_or_null($field['name'], '') ?? ($field['value'] ?? ($field['default'] ?? '')); if (!empty($currentValue)) { $currentValue = (function () use ($currentValue, $field, $optionsForModels) { $getValueFrom = function ($modelName, $value) use ($optionsForModels, $field) { if (!is_a($modelName, 'Illuminate\Database\Eloquent\Model', true)) { $modelName = $field['morphMap'][$modelName]; } // it's not an ajax morph option if (!array_key_exists($modelName, $optionsForModels)) { return; } $relatedModel = (new $modelName())->find($value); if ($relatedModel) { return [$relatedModel->getKey() => $relatedModel->{$optionsForModels[$modelName]['attribute']}]; } }; if (session()->has('morphTypeFieldValue')) { $modelName = session()->get('morphTypeFieldValue'); if (!empty($modelName)) { session()->remove('morphTypeFieldValue'); return $getValueFrom($modelName, $currentValue); } } })(); } //dd($optionsForModels); @endphp @include('crud::fields.inc.wrapper_start') {{-- HINT --}} @if (isset($field['hint']))

{!! $field['hint'] !!}

@endif @include('crud::fields.inc.wrapper_end') @push('crud_fields_scripts') @loadOnce('bpFieldInitMorphToSelectAjaxElement') @endLoadOnce @endpush {{-- End of Extra CSS and JS --}} {{-- ########################################## --}}