简体   繁体   English

如何从 Laravel 的 mysql 数据库中检索日期范围内的数据?

[英]How to retrieve data in a date range from mysql database in Laravel?

In my application, if someone clicks the Filter button I want to display data in a table format from mysql database, but here I am not receiving any responses.在我的应用程序中,如果有人单击“过滤器”按钮,我想以表格格式显示来自 mysql 数据库的数据,但在这里我没有收到任何响应。 Please someone help me out with this.请有人帮我解决这个问题。

custom_script.js custom_script.js

fetch_data();
    function fetch_data(from_date = '', to_date = '') {
        if(from_date != '' && to_date != '') {
               console.log(from_date+' | '+to_date)
        }

    }

    $('#filter').click(function() {
        var from_date = $('#from_date').val();
        var to_date = $('#to_date').val();
        if(from_date != '' && to_date != '') {
            fetch_data(from_date, to_date);
        }
        else {
            alert('Both Date is required');
        }
    });

    $('#refresh').click(function() {
        $('#from_date').val('');
        $('#to_date').val('');
        fetch_data();
    });

HolidayController.php假日控制器.php

public function fetch_data(Request $request)
    {

        if($request->from_date != '' && $request->to_date != '') {
            $data = DB::table('holidays')
                ->whereBetween('startdate', 
                array($request->from_date, $request->to_date))
                ->get();
        }
        else {
            $data = DB::table('holidays')->orderBy('startdate', 'desc')
            ->get();
        }
        return response($data);
    }

你可以像这样使用它

$holiday = Holiday::where('from_date', '>=', $date)->where('to_date', '<=', $date)->get();

Your fetch_data method in custom_script.js should be something like that as your previous script您在custom_script.js 中的fetch_data方法应该与您之前的脚本类似

function fetch_data(from_date = '', to_date = '') {
        $.ajax({
            url:"{{ route('holiday.fetch_data')}}",
            method:"POST",
            data:{
                from_date:from_date, to_date:to_date, _token:_token
            },
            dataType:"json",
            success:function(data) {
                var output = '';
                $('#total_records').text(data.length);
                for(var count = 0; count < data.length; count++) {
                    output += '<tr>';
                    output += '<td>' + data[count].id + '</td>';
                    output += '<td>' + data[count].firstname + '</td>';
                    output += '<td>' + data[count].lastname + '</td>';
                    output += '<td>' + data[count].startdate + '</td>';
                    output += '<td>' + data[count].enddate + '</td></tr>';
                }
                $('tbody').html(output);
            }
        })
    }

you used my script here,which i used for demonstration purpose.This is not workable code.You must need to make ajax request to the server to get some data as a response .您在这里使用了我的脚本,我将其用于演示目的。这不是可行的代码。您必须向server发出ajax请求才能获取一些数据作为response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM