@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') {{-- ########################################## --}} {{-- Extra CSS and JS for this particular field --}} {{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}} {{-- FIELD CSS - will be loaded in the after_styles section --}} @push('crud_fields_styles') @loadOnce('packages/select2/dist/css/select2.min.css') @loadOnce('packages/select2-bootstrap-theme/dist/select2-bootstrap.min.css') @endpush {{-- FIELD JS - will be loaded in the after_scripts section --}} @push('crud_fields_scripts') @loadOnce('packages/select2/dist/js/select2.full.min.js') @if (app()->getLocale() !== 'en') @loadOnce('packages/select2/dist/js/i18n/' . str_replace('_', '-', app()->getLocale()) . '.js') @endif @endpush @push('crud_fields_scripts') @loadOnce('bpFieldInitMorphToSelectAjaxElement') @endLoadOnce @endpush {{-- End of Extra CSS and JS --}} {{-- ########################################## --}}