如何从 Laravel 的 mysql 数据库中检索日期范围内的数据? [英] How to retrieve data in a date range from mysql database in Laravel?

查看:137
本文介绍了如何从 Laravel 的 mysql 数据库中检索日期范围内的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,如果有人单击过滤器"按钮,我想以表格格式显示来自 mysql 数据库的数据,但在这里我没有收到任何响应.请有人帮我解决这个问题.

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. Please someone help me out with this.

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

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

推荐答案

fetch_datacustom_script.js 中的方法应该是这样的作为您之前的脚本

Your fetch_data method in custom_script.js should be something like that as your previous script

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

你在这里使用了我的脚本,我用于演示目的.这是不可行的代码.你必须向服务器发出ajax请求来获取一些数据作为响应.

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.

这篇关于如何从 Laravel 的 mysql 数据库中检索日期范围内的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆