laravel 函数返回一个空数组

7lrncoxx  于 2023-11-20  发布在  其他
关注(0)|答案(2)|浏览(114)

我尝试使用从多个下拉列表中选择的选项显示从数据库中的respondent表中检索到的数据。尽管选项与数据库匹配,但当我console.log结果时,控制器仍然返回空数组。

控制器

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'  
        ]);
    }
}

字符串
这是JavaScript,并且当javascript的值改变时,javascript将触发并向控制器发送请求

<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();
    });
});


结果当我转储选定的选项从疾病

当我从疾病和疫苗中转储所选选项时的结果:

2nc8po8w

2nc8po8w1#

您可以转储请求数据和结果以进行调试:

dump($disease);
dump($vaccine);

$respondents = Respondent::where([
    'disease_id' => $disease,
    'vaccine_id' => $vaccine,        
])->dump()->get(); 

// This is the right check.
if ($respondents->isNotEmpty()) {
    return response()->json([
        'success' => true,
        'data' => $respondent  
    ]);
}

return response()->json([
    'success' => false,
    'message' => 'No matching record found'  
]);

字符串

yacmzcpb

yacmzcpb2#

你应该检查你的集合中是否有对象,使用:

dd($response->count());

字符串
你的情况:

if ($respondent)


->get()返回一个集合示例,即使没有结果。因此,您需要使用以下方法检查集合中是否有对象:

if ($respondent->isNotEmpty())


否则它将始终返回true

相关问题