1

I am having an issue getting rows from my table using the below query, the expected behaviour is:

user selects start/end date in a form, hits submit and the data passes to the function & it is then used in the query.

The data from the form is coming in fine & I have verifed the dates are coming into the query, but for some reason the query always returns zero results? When I strip out the WHERE statement for the dates, the query works fine & produces results, so the issue is to do with the dates.

$fromDate = $request->fromDate;
$toDate = $request->toDate;

$data = cc_table::all('*')
    ->where('link', '1')
    ->where('created_at', '>', $fromDate)
    ->where('created_at', '<', $toDate)
    ->toArray();

The dates from the form are formatted as dd/mm/yyyy, and the created_at date is a timestamp, however for testing I did change the created_at to be DATE (dd/mm/yyyy) however the query still returns zero rows.

FYI - I am just using jquery datepicker on my form:

 $(function() {
    $( "#fromDate" ).datepicker();
  });
  </script>
1
  • why not use yyyy-mm-dd all the way through? Commented Jun 23, 2016 at 0:37

1 Answer 1

2

Have a try with this:

$fromDate = Carbon::parse($request->input('fromDate'))->format('Y-m-d');
$toDate = Carbon::parse($request->input('toDate'))->format('Y-m-d');
$date_range = [$fromDate . ' 00:00:00', $toDate . ' 23:59:59'];

$data = cc_table::where('link', '1')
->whereBetween('created_at', $date_range)
->get();

The format of $date_range will depend on the type of your MySQL column.

Note: be sure to use Carbon\Carbon; at the top of the script with the above code snippet.

Sign up to request clarification or add additional context in comments.

2 Comments

That is brilliant and worked perfectly, thanks for your help on this.
Also, thanks for the 'whereBetween' - I overlooked this on the laravel Docs. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.