Get Only Records Created Today in Laravel

Get only records created today in laravel

For Laravel 5.6+ users, you can just do

$posts = Post::whereDate('created_at', Carbon::today())->get();

How to retrieve data of current date in Laravel

You can use that;

PrescriptionTbl::whereDate('created_at', date('Y-m-d'))->get();

Laravel Check if Any Records Exist with Today's Date

You can use laravel eloquent whereDate to get the record created today.

Eg: $query->whereDate('created_at', today())

you can do something like

Appointments::whereDate('date', today((auth()->user()->timezone)))
->where('user_id', auth()->user()->id)
->count();

or

Auth::user()
->appointments
->whereDate('date', today(Auth::user()->timezone)))
->count();

Laravel 5.4: How do I get records from just this week?

Try this:

Data::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get();

To set the week start/end:

Carbon::setWeekStartsAt(Carbon::SUNDAY);
Carbon::setWeekEndsAt(Carbon::SATURDAY);

How to retrieve data from the latest date in laravel?

use orderbBy():

TblModel::orderBy('date','DESC')->first();

Or

DB::table('tbl')->orderBy('date', 'DESC')->first();

Update:

TblModel::where('date', TblModel::max('date'))->orderBy('date','desc')->get();

Laravel get rows with created_at not older than 24 hours

Less than 24 hours is:

['created_at', '>=', Carbon::now()->subHours(24)->toDateTimeString()]

With >=, not <=

Also be sure you use the same timezone when you save in DB and when you SELECT them.

Side note, 1 day is not 24 hours, (it can be 23 or 25 due to DST and even 22 and 26 in Antarctica). If you can, use UTC everywhere in your back-end.

How to get all records for current year?

You can do this:

Articles::whereBetween('created_at', [
Carbon::now()->startOfYear(),
Carbon::now()->endOfYear(),
]);

Another way is to use whereYear() as in @xhulio answer. But I'd recommend you to use this code instead as it's more readable:

Articles::whereYear('created_at', date('Y'))->get();

Laravel Eloquent find rows where date older than 2 days

You can use Carbon subDays() like below:

$data = YOURMODEL::where('created_at', '<=', Carbon::now()->subDays(2)->toDateTimeString())->get();

Laravel Select records in specific year

You can do it like that:
$posts = Blog::where( DB::raw('YEAR(created_at)'), '=', '2015' )->get();

Here you can get year from created_at field with YEAR function, then compare with your date.



Related Topics



Leave a reply



Submit