Laravel Eloquent find rows where date older than 2 days - php

I'm trying to fetch rows based on the "created_at" field that are older than (for example) 2 days.
I'm using Laravel Eloquent. Can somebody help me with this?

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

You can use the whereDate() eloquent method to run a query against dates.
Model::whereDate('created_at', '<=', now()->subDays(2)->setTime(0, 0, 0)->toDateTimeString())->get();
The now() is a laravel helper function to get a carbon instance of the current date and time.
NOTE: We set the time using the setTime() method so that it starts from the beginning of the day.
Update: It is also possible to use ->startOfDay().

Related

Match only day and month and show records regardless of year in Laravel Eloquent

I only want to fetch the records that were created any year of a given date regardless of the year in Laravel 9. I tried the query below.
Posts::query()->where([DAYOFMONTH(created_at)=>date('d', strtotime($dated)), MONTH(created_at)=>date('m', strtotime($dated))])->paginate(10);
How can I match only day and month and fetch the records? Thanks in advance
There are bunch of built in functions for building a query that work with dates and times:
whereDate
whereMonth
whereDay
whereYear
whereTime
$posts = DB::table('posts')->whereMonth('created_at', '10')->whereDay('created_at', '10')->paginate(15)->get();
read more here

Illuminate query builder, how to convert dates to Carbon object

Is there any chance to use Illuminate Query builder to get dates as Carbon objects instead of strings?
For example:
$user=DB::table('users')->select(["id","lastLogin"])->where("id",1)->first();
$user->lastLogin; // <--- Carbon instead of string!
You can use Carbon::parse($user->lastLogin), but I think there is no native way to get dates as carbon objects without using eloquent. An example is this answer on stackoverflow.
I think working with eloquent models will make the work much easier. So you can set the $dates property to get the lastLogin as carbon object. More informations at laravel docs.

get records from database base on the specified date range laravel 5

How can I retrieved records from the database using carbon dates with the specified date range e.g retrieved records from "2014-10-13" to "2015-11-18"? any ideas, help, suggestions, clues, recommendations please?
You can use the whereBetween method. So, just as an example, lets say you have two Carbon dates.
$earlier = Carbon::yesterday();
$later = Carbon::today();
Model::whereBetween('column', [$earlier, $later])->get();
The first parameter represents the column you are checking. The second parameter is an array of the two dates.
Using the dates in your question, you can generate the carbon dates like this:
$earlier = Carbon::parse('2015-09-11');
$later = Carbon::parse('2015-09-14');
Model::whereBetween('column', [$earlier, $later])->get();
The parse method is pretty awesome.

Calculate date diff in Eloquent where clause Laravel 5

In my Laravel 5 app I've got a relationship, and from this one-to-many relationship, I want to query and get only results whose date diff between their creation_date and an arbitrary date is between 0 and 2 days.
If you need I can show you a pseudocode, even if I think that it's clear what I need.
Thank you!
Just create a arbitrary date using carbon (as you are using laravel):
$start = Carbon\Carbon::create(year,month,day);
$end = $start->copy()->addDays(2);
And a little DB::raw in the mix, to format the created_at field to your needs:
Model::where(DB::raw('DATE_FORMAT(created_at,"%Y-%m-%d")'),'>=',$start)->where(DB::raw('DATE_FORMAT(created_at,"%Y-%m-%d")'),'<=',$end)->get();
That's it.

Using Laravel and Carbon

I have a field in my database called "deadline" which is of DATE format, and I want to use Eloquent to say that if the deadline field does not match Carbon::now(); but isn't in the future then don't show this row.
How can I achieve this?
Select records where deadline is greater than or equal to today's date() in the Y-m-d format.
Model::where('deadline','>=', date('Y-m-d'))->get();
Laravel works with Carbon for formatting timestamps etc, which you can easily set the deadline column as one of those Carbon objects with a Laravel date mutator allowing you formatting abilities.
But for a select statement, i'd just use the above personally.

Categories