使用多选下拉筛选数据时Controller返回空数组求助
多下拉菜单筛选respondent表数据时,选中项匹配但Controller返回空数组
我想通过多个下拉菜单的选中值从数据库的respondent表中获取数据并展示,但明明选中项和数据库内容匹配,控制台打印结果时Controller却返回空数组。
Controller代码
public function retrieveRowFromDatabase(Request $request) { $disease = $request->input('disease'); $vaccine = $request->input('vaccine'); $category = $request->input('category'); $envHazards = $request->input('env_hazards'); $age = $request->input('age'); $condition = $request->input('condition'); $sex = $request->input('sex'); $purok = $request->input('purok'); $start = $request->input('start'); $end = $request->input('end'); $respondent = Respondent::where([ 'disease_id' => $disease, 'vaccine_id' => $vaccine, ])->get(); if ($respondent) { return response()->json([ 'success' => true, 'data' => $respondent ]); } else { return response()->json([ 'success' => false, 'message' => 'No matching record found' ]); } }
触发AJAX请求的JavaScript代码
当下拉菜单值改变时会向Controller发送请求:
<script type="text/javascript"> $(document).ready(function() { function retrieveRowFromDatabase() { var selectedValues = { disease: $('#disease').val(), vaccine: $('#vaccine').val(), category: $('#category').val(), env_hazards: $('#env_hazards').val(), age: $('#age').val(), condition: $('#condition').val(), sex: $('#sex').val(), purok: $('#purok').val(), start: $('#start').val(), end: $('#end').val() }; console.log(selectedValues) $.ajax({ url: '/retrieveRowFromDatabase', type: 'POST', data: { selectedValues, _token: '{{csrf_token()}}' }, dataType: 'json', success: function(response) { if (response.success) { var rowData = response.data; console.log(rowData); $('#respondentTable tbody').empty(); var row = '<tr>'; for (var key in rowData) { if (rowData.hasOwnProperty(key)) { row += '<td>' + rowData[key] + '</td>'; } } row += '</tr>'; $('#respondentTable tbody').append(row); } else { console.error('Failed to retrieve row from the database.'); } }, error: function(xhr, status, error) { console.error('AJAX request error:', error); } }); } $('#disease, #vaccine, #category, #env_hazards, #age, #condition, #sex, #purok, #start, #end').on('change', function() { retrieveRowFromDatabase(); }); }); </script>
控制台打印的选中项
- 仅选中疾病时的打印结果:

- 选中疾病与疫苗时的打印结果:

问题原因及解决办法
请求参数层级错误
AJAX里把所有选中值包装在了selectedValues对象中,但Controller直接用$request->input('disease')取值,会取不到实际参数——因为参数实际在selectedValues.disease层级下。- 方案一:修改AJAX的
data字段,平铺参数:data: { disease: $('#disease').val(), vaccine: $('#vaccine').val(), category: $('#category').val(), env_hazards: $('#env_hazards').val(), age: $('#age').val(), condition: $('#condition').val(), sex: $('#sex').val(), purok: $('#purok').val(), start: $('#start').val(), end: $('#end').val(), _token: '{{csrf_token()}}' }, - 方案二:在Controller里从
selectedValues对象取值:$disease = $request->input('selectedValues.disease'); $vaccine = $request->input('selectedValues.vaccine'); // 其他参数同理修改
- 方案一:修改AJAX的
空值参数导致筛选异常
未选中的下拉菜单会传入空值,此时where条件会严格匹配空值,导致无结果。需要判断参数是否有值,再添加筛选条件:$query = Respondent::query(); if ($disease) { $query->where('disease_id', $disease); } if ($vaccine) { $query->where('vaccine_id', $vaccine); } // 其他需要用到的筛选参数同理添加判断 $respondent = $query->get();查询结果判断逻辑错误
Laravel的get()返回的是Collection对象,即使无数据,if ($respondent)也会判定为true。正确判断方式是用isEmpty():if (!$respondent->isEmpty()) { return response()->json([ 'success' => true, 'data' => $respondent ]); } else { return response()->json([ 'success' => false, 'message' => 'No matching record found' ]); }
内容的提问来源于stack exchange,提问作者Big Brother
相关产品推荐
相关产品推荐

