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
List All Employee'S Names and Their Managers by Manager Name Using an Inner Join
Counting the Number of Rows Returned by Stored Procedure
Query to Get All Those Names of Employees,Who Have 'A' as Their Middle Character in Their Name
How to Close Idle Connections in Postgresql Automatically
Mysql Select Rows on First Occurrence of Each Unique Value
Postgresql How to Insert a Value With Double Quotes Inside Double Quotes
Multiple Query Same Table But in Different Columns MySQL
Remove Multiple Keys from Jsonb Column in One Statement
What's the Best Way to Capitalise the First Letter of Each Word in a String in SQL Server
Multiplying Two Columns in SQL Server
How to Directly View Blobs in MySQL Workbench
Mysql - Operand Should Contain 1 Column(S)
Show Zero If There Is No Record Count - Oracle SQL Query
Add a Column to Specific Position in Mssql Server
Simple Check for Select Query Empty Result
Getting Student Name With Highest Total Mark in SQL
Get the Number of Digits After the Decimal Point of a Float (With or Without Decimal Part)