Constantine_92's avatar

hi Dropdown dependent with ajax!

Hi i try to associated 3 models 1)Specialities 2)Surveys 3)Works

Each specialities have many surveys that can declaration . That mean that each surveys have many same specialities_id (how i do that without duplicated rows? )

After each surveys belong to specific work

0 likes
10 replies
Sinnbeck's avatar

Livewire? Vue? React? jQuery? Alpine? Vanilla javascript?

Do you have some code to show?

Constantine_92's avatar

@sinnbeck //Route

	Route::get('getCourse/{id}', function ($id) {
    $survey = App\Models\Survey::where('specialties', $id)->get();
    return response()->json($survey);
});
Sinnbeck's avatar

@Constantine_92 No it just ensures that a query only returns unique rows (no duplicates)

Route::get('getCourse/{id}', function ($id) {
   $survey = App\Models\Survey::where('specialties', $id)->distinct()->get();
   return response()->json($survey);
Constantine_92's avatar

@Sinnbeck @tray2 guys that work for those models specialities and survey how to associated the third model works to survey ? works have 10 rows and belongs to survey at survey i have 100 row and 100 ids ? Any idea ?

Constantine_92's avatar

@sinnbeck Blade

			<div class="form-group">
            <label for="speciality_id">{{ trans('cruds.experience.fields.speciality') }}</label>
            <select class="form-control select2 {{ $errors->has('speciality') ? 'is-invalid' : '' }}" name="speciality_id" id="category">
                @foreach($specialities as $id => $entry)
                    <option value="{{ $id }}" {{ old('speciality_id') == $id ? 'selected' : '' }}>{{ $entry }}</option>
                @endforeach
            </select>
            @if($errors->has('speciality'))
                <div class="invalid-feedback">
                    {{ $errors->first('speciality') }}
                </div>
            @endif
            <span class="help-block">{{ trans('cruds.experience.fields.speciality_helper') }}</span>
        </div>
        <div class="mb-3">
            <label for="survey" class="form-label">Survey</label>
            <select class="form-control" name="survey_id" id="survey"></select>
        </div>

//script

				<script>
$(document).ready(function() {
$('#category').on('change', function() {
   var categoryID = $(this).val();
   if(categoryID) {
       $.ajax({
           url: '/admin/getCourse/'+categoryID,
           type: "GET",
           data : {"_token":"{{ csrf_token() }}"},
           dataType: "json",
           success:function(data)
           {
             if(data){
                $('#survey').empty();
                $('#survey').append('<option hidden>Κατηγορία Μελετών</option>'); 
                $.each(data, function(key, survey){
                    $('select[name="survey_id"]').append('<option value="'+ survey.specialties +'">' + survey.name + '</option>');
                });
            }else{
                $('#survey').empty();
            }
         }
       });
   }else{
     $('#survey').empty();
   }
});
});

The problem that i face is: i have 75 specialities and 27 surveys but 1 specialities have at least 3 surveys that declaration. I want in my database at surveys not have duplicated rows for example nameofsurvey= ex | specialities_id = 1. nameofsurvey= ex | specialities_id = 2. nameofsurvey= ex | specialities_id = 7.

Please or to participate in this conversation.