mohammad-fouladgar/eloquent-builder

How to use search between dates?

multiarts opened this issue · 6 comments

Congratulations on this beautiful package.

I have a dt_start column of type date, in the form I have the inputs from_date and to_date, in the url displays /called?from_date=2020-07-09&to_date=2020-07-23
But it shows the error Not found the filter: FromDateFilter, for not having the FromDate filter, of course. I created a DtStart filter.

public function apply(Builder $builder, $fromDate): Builder
{
    return $builder->whereBetween('dt_start', [$fromDate, $fromDate]);
}

So, how do I get these two parameters?

@multiarts Thanks a lot.
First, make a DateFilter class. Then call a url like this:
/called?date[from_date]=2020-07-09&date[to_date]=2020-07-23
Finally, make your query:

public function apply(Builder $builder, $date): Builder
{
    list('from_date'=>$fromDate, 'to_date'=>$toDate) =$date;
     
    return $builder->whereBetween('dt_start', [$fromDate, $toDate]);
}

I don't access to my PC for testing the above code at the moment.
Please note that, you need to test the code your self before using them.

Typing in the URL works, but when I submit the form it looks like this in the address bar.

/called?date%5Bfrom_date%5D=2020-07-14&date%5Bto_date%5D=2020-07-22

And in the form I modified the inputs.

<input name="date[from_date]"> <input name="date[to_date]">

If you can help me, I will be immensely grateful.

  1. I need to search between dates - OK
  2. By status: / called & status = 1 - OK
  3. Per month: / called? Month = 08 - OK
  4. Per user: / called? User_id = 1 - OK
  5. By tecnico: / called? Tecnico_id = 5 - OK

When I type in the url separated as above or /called?month=2&status=4 or /called?month=08&status=3&tecnico_id=2
works great, but if the date comes empty it returns nothing.

/called?date%5Bfrom_date%5D=&date%5Bto_date%5D=&month=08&status=2

How do I make the form send only what was selected?

@multiarts First of all, you should fix you form as below (You missed one closing bracket):
<input name="date[from_date]"> <input name="date[to_date]">

Then, you should know that, it is not possible to use WhereBetween without one null/empty parameter. So you have to send start and end date.

@multiarts We will fix this bug soon.

@multiarts Version 2.2.1 released and fixed this issue. Please use the latest version.

@mohammad-fouladgar Thank you very much for your attention and quick solution, it is now working as I wish.