FareedR
5 months ago

Getting data using with() relation in controller to js

Posted 5 months ago by FareedR

How can i get campus data ?

// db
universities
id | subdomain |

campus
id | university_id | name |

// script
<script>
            $('#btnSearch').on('click',function (event){
        // getting subdomain here
                var university = $('#university').val()
                $.ajax({
                    type: "GET",
                    url: "{{ url('/get-university') }}" + '/' + university,
                    data: {university : university},
                    success: function(data){
                        $('#availableCampus').empty()
                        $.each(data,function(index, campusObj){
                            $('#availableCampus').append('<option value="{{'+campusObj.id+'}}">'+campusObj.name+ '</option>')
                        });
                    },
                    error: function(){
                        swal({
                            title: "Opps",
                            text: "University doesnt exist",
                            icon: "error",
                            timer: 1500
                        })
                    }
                })
            });
        </script>

//controller
public function getUniversity($university)
    {
        $availableCampus = University::with('campuses')->where('subdomain','=',$university)->get();
        return \Response::json($availableCampus);
    }

Please sign in or create an account to participate in this conversation.