Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kuns25's avatar

Need to Learn Date Range Filter in Laravel

Hii I have simple registered user table , i want to add Add Range Filter Where admin can select From & To Date From Date picker // Calendar . Is there any tutorials on youtube , laracast , or anywhere can i see some sample demo code . please suggest .

Thanks & Regards,

0 likes
11 replies
bobbybouwmann's avatar
Level 88

The basic gist of it is that you need to parameters in request. The from and to date. You can simply do this in a form for example or you can get them as query parameters to your url.

In your controller you have to get those values from the request and use them in your query. Laravel offers you a `whereBetween(field, array of values).

So you might end up with something like this in your controller

public function index(Request $request)
{
    $users = User::whereBetween('created_at', [$request->get('from'), $request->get('to')])->get();

    return redirect()->route('users.index', compact('users'));
}

Documentation: https://laravel.com/docs/5.7/queries#where-clauses

2 likes
jlrdw's avatar

Can share Blade File please.

You are not willing to learn some blade on your own?

And if something does not go right you could post what you have tried for more help.

bobbybouwmann's avatar

Well you need some javascript I guess to make this work in a nice way for the user. However this blade should be enough

<form action="" method="GET">
    <input type="text" name="from" placeholder="from">
    <input type="text" name="to" placeholder="to">
    <button type="submit">Search</button>
</form>

Note: The inputs are now text so the user can fill in whatever they want. . However there are a lot of javascript libraries to make a date(time) picker if you wish.

1 like
Cronix's avatar

Ain't it great when browsers don't follow the specs. HTML5 is what, only 4 years (and 3 days) old now as of this month?

However, html5 controls degrade to just <input type="text"> if the browser doesn't support it so it's not like it will break. You just won't get a pretty widget to enter the data. If you click on the Usage Relative tab, it's really a tiny minority of users it would affect.

1 like
bobbybouwmann's avatar

@cronix Correct. However a lot of different users is a lot of different behaviour! Unless all your users use a proper browser ;)

1 like
jlrdw's avatar

@mac03733 it depends, me use the jquery datepicker, it is so easy.

https://api.jqueryui.com/datepicker/

And if user has js off, have the example date format they are to use next to the date field.

However

If you have a business app, you could have a list of browsers you want people to use.

Date picker usage is just this simple:

        <script>
            $(document).ready(function () {
                $(".odate").datepicker({
                    showOn: "button",
                    buttonImage: "/crudv22/public/overcast/images/calendar19.gif",
                    buttonImageOnly: true,
                    dateFormat: "yy-mm-dd",
                    changeMonth: true,
                    changeYear: true
                });
            });
        </script>       

Of course you have to load jquery, the ui, and a theme if needed. Search the web or youtube, you'll probably find a good tutorial on setting all of this up. But really it is really easy to set all up.

1 like

Please or to participate in this conversation.