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

Kavyajain's avatar

Laravel Eloquent get Date from DateTime

$request->from = somedate; $request->to = somedate; $xyz = Reports::with('doctors')->whereDate('created_at',[$request->from,$request->to])->get();

How do i fetch records which lie between from and to date, where created is in dateTime format.

0 likes
7 replies
Sergiu17's avatar
DB::table('doctors')->whereBetween('created_at', [$request->from, $request->to])->get();
// or
Reports::with('doctors')->whereBetween('created_at', [$request->from, $request->to])->get();

Is this working?

1 like
ChrisSFR's avatar

Eloquent has a whereBetween api method. https://laravel.com/docs/5.6/queries#where-clauses

Reports::with('doctors')->whereBetween('created_at',[$request->from,$request->to])->get();

When your request date format is not in the correct format, then you can use Carbon to parse it in the right format.

$dt = Carbon::parse($request->to);
$dt->toDateTimeString();
Kavyajain's avatar

No..none of this is working. $report_info = Reports::with('masters')->whereDate('created_at','>=',$request->from)->orWhereDate('created_at','<=',$request->to)->get();

ChrisSFR's avatar

Take care with your chaining:

when you append orWhereDate than only this condition must be true. For your between check you must chain two whereDate conditions.

Reports::with('masters')->whereDate('created_at','>=',$request->from)->whereDate('created_at','<=',$request->to)->get();

Can you post your request()->to input format

1 like
Kavyajain's avatar

Yes. This works. Thanx

$start = Carbon::parse($request->from)->format('Y-m-d');

$end = Carbon::parse($request->to)->format('Y-m-d');

$report = Reports::with('booking')->whereDate('created_at','>=',$start)->WhereDate('created_at','<=',$end)->get();

created_at date format was different so had to parse it initially.

hamzakhan17826's avatar

If i want to get only date from created_at column. Then how to write query for this purpose?

Please or to participate in this conversation.