Retrieve records by date laravel - php

I have several records in a model, and want to be able to retrieve them by date. My url looks like this:
Nov-01-2017/answers and is generated with the following:
/{{$date->format('M-d-Y')}}/answers"
The created_at format looks like: 2017-11-01 16:58:04.
My controller looks like this:
$record = $record->where('created_at', '=', $date)->get();
It won't retrieve the records and I'm sure it's something to do with my date formatting but any advice on how to retrieve records based on the created_at date would be helpful!
Thanks

Have you tried
$record = $record->whereDate('created_at', '=', date('Y-m-d', $date))->get();

You can use:
$record = $record->whereDate('created_at',
Carbon::createFromFormat('M-d-Y', $date)->toDateString())->get();
So you create carbon based on given format and then you change this into valid string in Y-m-d format.

Related

Laravel: get users created more than three months ago

in my laravel app I want to get all records of users where "created_at" timestamp is older than three months from current date, is it possible to achieve this with Eloquent query builder?
First, you can use Carbon to get the date of 3 months ago.
$date = Carbon::now()->subMonths(3)->format('Y-m-d');
And then we need all the records before that that. So, query should be-
$users = User::where('created_at', '<=', $date)->get();
belowe code can help you to solve problem
$three_month = Carbon::now()->startOfMonth()->subMonth(3);
$data = DB::table('orders')
->where('placed_at','>',$three_month)
->get();
$dateS = Carbon::now()->startOfMonth()->subMonth(3);
$dateE = Carbon::now()->startOfMonth();
$usersArr = $users
->whereBetween('created_at',[$dateS,$dateE])
->get();
startOfMonth() begins with 1st date of the month
To find users older than three months
$users = User::where('created_at','<', today()->subMonths(3))->get();
The easiest way to make it done by using carbon. Use the below code-
User::where( 'created_at', '<=', Carbon::now()->subMonths(3))->get();
Eloquent can compare datetimes. You can use Carbon to get the date you want to compare to and use it in your eloquent query.
$threeMonthsAgo = now()->subMonth(3);
$users = User::where('created_at', '<' , $threeMonthsAgo)->get();

How do I make the unique check on month of a year in Laravel

Here is a table named installments which has date_of_receipt and user_id columns. I need to check if a date date_of_receipt = 27-01-2019 has present according to a user so I have to validate this entity already exists here for any January 2019 day. How can I do that in Laravel5.7 with MySQL?
Because you are storing it as a string in a particular format, you could achieve this with something like this:
$start = \Carbon\Carbon::parse($request->date_of_receipt)->startOfMonth()->format('d-m-Y');
$end = \Carbon\Carbon::parse($request->date_of_receipt)->endOfMonth()->endOfDay()->format('d-m-Y');
return DB::table('installments')
->where('user_id', $user_id)
->whereBetween('date_of_receipt', [$start, $end])
->exists();

Laravel Carbon, querying database where a field date is 2 weeks less than itself

I have a Election model, where I'm trying to write a query that will show me specific results.
The idea is pretty simple:
A Election starting_date, for example is 15/10/2018.
I need my query to show all elections that will start in next 2 weeks.
I mean, for that specific case, today is 01/10/2018, so I need all elections that will start in period 01/10/2018 - 15/10/2018.
So, I tried to write something like:
public function notificationBeforeCollection() {
return $this->activeElections()
->where('start_collection', '>=', Carbon::now()
->subDays(14)->format('Y-m-d'))
->where('start_collection', '<', Carbon::now()
->format('Y-m-d'));
}
But it doesn't work, and it looks like it can't work by comparing the starting_date with today. It looks like I need to write something like:
where('starting_date', '>=', 'starting_date'->subDays(14);
If I'm right, is there a way how to use Carbon against a field in query builder?
Your actual query is looking for all elections that started 14 days ago.
You need to do something like:
return $this->activeElections()->where('start_collection', '>', Carbon::today())
->where('start_collection', '<=' Carbon::today()->addDays(14));
To get 'Elections' of two weeks from tomorrow
// using 'Carbon'
$start_date = Carbon::now()->addDay()->format('Y-m-d');
$end_date = Carbon::now()->addDays(14)->format('Y-m-d');
public function notificationBeforeCollection() {
return $this->activeElections()->whereBetween(
'start_collection', [$start_date, $end_date]
);
}

Laravel Eloquent whereDate() with UnixTimestamps

the entries in my database have a custom timestamp field (e.g. 1488446089).
How do I use whereDate() or whereDay() with unix timstamps?
This of course just works for Date Fields.
$posts = Post::whereDay('uploaded_time', '=', '2017-05-10')->get();
To run it correctly use whereDate instead of whereDay
$posts = Post::whereDate('uploaded_time', '2017-05-10')->get();
To get all specific day means (date of month like 1-30)
$posts = Post::whereDay('uploaded_time', '10')->get();//provide date of month
get more detail : https://laravel.com/docs/master/queries#where-clauses
One way to do this is fetching all Posts, and then filtering the results in PHP:
$myDate = '2017-05-10';
$posts = Post::all()->filter(function (Post $post) use ($myDate) {
$uploadedAt = Carbon::createFromTimestamp($post->uploaded_time)->format('Y-m-d');
return $uploadedAt === $myDate;
});
Why? Because if I'm right you are trying to fetch all posts that were uploaded on a certain date. This means that there is a range of timestamps valid. (60 * 60 * 24). With Carbon (comes with Laravel) you can easily convert your timestamps to Y-m-d formatted strings. Those you then compare to the date string of your choice.
Edit
I have no way to test this at the moment, but it should be possible to do this with a query builder after all:
$myDate = Carbon::createFromFormat('Y-m-d', '2017-05-10');
$startOfDayTimestamp = $myDate->startOfDay()->timestamp;
$endOfDayTimestamp = $myDate->endOfDay()->timestamp;
$posts = Post::whereBetween('uploaded_time', [
$startOfDayTimestamp,
$endOfDayTimestamp
])->get();
Here we create 2 timestamps, one for the very start of the date you wish to filter by, and one for the very end. After that, we can use the builder's whereBetween() method and pass the 2 timestamps.
I found this solution here, and simplified a little bit by replacing the setTime() calls with startOfDay and endOfDay methods.

Query the DB DATETIME column just by date

I have a column in the DB with DATETIME type and is with format yyyy-mm-dd hh:mm:ss. Nevertheless, as information I am given just the date (and I don't care about the time). So how should I query the DB to search that column just by date, even though the value contains both the date and the time?
Update:
Here's the PHP code where I am trying to select the entities:
public static function getTripsWithDrivers($route_from = null, $route_to = null, $date = null)
{
$trips = Trip::whereRouteFrom($route_from)->whereRouteTo($route_to)->whereStartDate($date);
...
}
Use MySql date function:
$trips = Trip::where(DB::raw('DATE(route_from)'), '=', $route_from)
->where(DB::raw('DATE(route_to)'), '=', $route_to)
->where(DB::raw('DATE(start_date)'), '=', $date)
User where filter as the following:
WHERE "datetime column" like '2015-04-06%'

Categories