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>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.