noblemfd
5 months ago
665
5
Laravel

How to load employee name based on dropdown change

Posted 5 months ago by noblemfd

In my Laravel project I want to load employee fullname based on dropdown change of the selected employee.

Controller:

   public function findEmployee(Request $request){
     $data =  HrEmployee::select(DB::raw('CONCAT(employee_code, " - ", first_name, " ", last_name) AS full_name'))->where('id', $request->id)->get()->pluck('full_name');

    return response()->json($data);

  }

route/web.php

Route::get('get/findEmployee','[email protected]')->name('get.employee.all');

view

          <div class="col-sm-4">
            <div class="form-group" method = "get">
                <select class="form-control select2bs4" data-placeholder="Select Employees tabindex="1" id="employee_id" name="employee_id" onchange="this.form.submit()">
                    <option value="select">Select Employees</option>
                @foreach($managerfilters as $managerfilter)
                    <option value="{{ $managerfilter->id }}" {{ request()->input('employee_id') == $managerfilter->id ? 'selected' : '' }}>{{ $managerfilter->employee_code }} - {{ $managerfilter->first_name }} {{ $managerfilter->last_name }}</option>
                @endforeach
                 </select>
            </div>
          </div>   

javascript

   <script type="text/javascript">
    $(document).ready(function() {
        $(document).on('change', '#employee_id', function() {
            var air_id =  $(this).val();

            var a = $(this).parent();
            var op = "";
            
            $.ajax({
                type: 'get',
                url: '{{ route('get.employee.all') }}',
                data: { 'id': air_id },
                dataType: 'json',      //return data will be json
                success: function(data) {
                    console.log(data);
 
                     $('#employee_name').val(data);
                     },
                error:function(){

                }
            });
        });
    });
    </script>

On dropdownlist change, I expected to see the full_name of the selected employee on

               <span class="font-weight-bold">
                    For: <input id="employee_name" class="form-control" value="" readonly>

                </span>

but I got this error:

HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE

How do I resolve it?

Thanks

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