动态下拉laravel服务器错误

时间:2018-07-14 04:11:32

标签: laravel server-error

我正在尝试使用另一个下拉菜单来获取动态下拉菜单。这是我的blade.php文件

<div class="form-group">
    <select name="companies" id="company_id" class="form-control input-lg dynamic" data-dependent="program_id">
        <option value="">Select Company</option>
        @foreach($companies as $company)
        <option value="{{$company->company_id}}">{{$company->company_name}}</option>
        @endforeach
    </select>
    </br>
    <select name="program_id" id="program_id" class="form-control input-lg">
        <option value="">Select Program</option>

    </select>
    </div>   
    {{ csrf_field() }}

    <script>
    $(document).ready(function(){
        $('.dynamic').change(function(){
            if($(this).val() != '')
            {
                var select = $(this).attr("id");
                var value = $(this).val();
                var dependent = $(this).data('dependent');
                var _token = $('input[name = " "]').val();

                $request = $.ajax({
                    url:"{{ route('dynamicdependent.fetch') }}",
                    method:"POST",
                    data:{'_token':_token, 'select':select, 'value':value, 'dependent':dependent},
                    success:function(result)
                    {
                        $('#'+dependent).html(result);
                    }
                })
            }
        });
    });
    </script>

这是我在控制器文件中的提取功能。

function fetch(request $request)
    {
        //try{
            $_token = $request->get('$data->_token');
            $select = $request->get('$data->select');
            $value = $request->get('$data->value');
            $dependent = $request->get('$data->dependent');

            echo $select;

            $data = DB::table('company_programs')
                    ->where($select, $value)
                    ->groupBy($dependent)
                    ->value('program_id')
                    ->get();

            $output = '<option value="">Select '.ucfirst($dependent).'</option>';
            foreach($data as $row)
            {
                $output = '<option value="'.$row->$dependent.'">'.$row->$dependent.'</option>';
            }

            echo $output;
        //}

        //catch(\Exception $e)
        //{
            //console.log($e);
        //}
    }

这是我的路线文件

Route::get('/dynamicdependent', 'DynamicDropDownController@index');
Route::post('/dynamicdependent/fetch', 'DynamicDropDownController@fetch')->name('dynamicdependent.fetch');

每当我在“公司”下拉菜单中选择值时,“程序”下拉菜单都不响应,并且出现500个内部服务器错误。

0 个答案:

没有答案