Please wrap the code block in three ` (backtics) to make it readable.
Can't figure out how to display subjects of a specific year with dropdown
I am trying to figure out how to get all the subjects of a specific year with ajax, jquery in Laravel. I have three tables:
years: id, name subjects: id, name year_subject: id, year_id , subject_id
Here is the function in controller
public function get_subjects($id) {$sub = YearSubject::where("year_id", $id)->pluck("subject_id"); $subjects=Subject::where('$id',$sub)->pluck('name','id'); foreach($sub as $s)
{
$subjects= Subject::where('$id',$s)->pluck('name','id');
}
return json_encode($subjects);
}
Here is the script
$(document).ready(function () { $('#grade').on('change', function () { var gradeID = $(this).val(); $('#subject').empty(); if (gradeID) { $.ajax({ url: '{{url('get_subjects')}}' + '/' + gradeID, type: "GET", dataType: "json", success: function (data) { $.each(data, function (key, value) { $('#subject').append('' + value + ''); }); } }); } else { $('#subject').empty(); } }); });The view
Choose Year @foreach($grades as $grade) {{$grade->name}} @endforeach Choose Subject
Route:
Route::get('get_subjects/{id}', 'SubjectController@get_subjects')->name('get_subjects')
I guess the error is at the controller. Can't figure out how to push values at $subjects.
Please or to participate in this conversation.