How to get last 7 days records with 0 counts - laravel

I have an eloquent query that gets the total count records (created_at) of the last 7 days. But the problem is if one of these days have 0 records, this doesn't appear in the final data.
My query:
$data = Data::whereBetween('created_at', [Carbon::now()->subDays(6)->format('Y-m-d')." 00:00:00", Carbon::now()->format('Y-m-d')." 23:59:59"])
->groupBy('date')
->orderBy('date')
->get([
DB::raw('DATE(created_at) as date'),
DB::raw('count(*) as total')
])
->pluck('total', 'date')->toArray();
What I get:
[
"2020-04-14" => 1
"2020-04-16" => 1
"2020-04-18" => 1
"2020-04-19" => 1
]
What I expected:
[
"2020-04-14" => 1
"2020-04-15" => 0
"2020-04-16" => 1
"2020-04-17" => 0
"2020-04-18" => 1
"2020-04-19" => 1
"2020-04-20" => 0
]
Any suggestions?
SOLUTION:
-Based on Gary Houbre's proposal:
$results = Data::whereBetween('created_at', [Carbon::now()->subDays(6)->format('Y-m-d')." 00:00:00", Carbon::now()->format('Y-m-d')." 23:59:59"])
->groupBy('date')
->orderBy('date')
->get([
DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d") as date'),
DB::raw('count(*) as total')
])
->keyBy('date')
->map(function ($item) {
$item->date = Carbon::parse($item->date);
return $item;
});
$period = new DatePeriod(Carbon::now()->subDays(6), CarbonInterval::day(), Carbon::now()->addDay());
$graph = array_map(function ($datePeriod) use ($results) {
$date = $datePeriod->format('Y-m-d');
return $results->has($date) ? $results->get($date)->total : 0;
}, iterator_to_array($period));

Looking directly Sql : How to include "zero" / "0" results in COUNT aggregate?
Into a same table : How to get the record if Count is zero in Laravel
You need to add an outer join into your request with Eloquent.

My idea is to create a for loop to check the days.
If there is no record on a date then print 0
Loop Iteration:
Catch the first Day (Suppose 14)
Catch the last Day
Then check in every iteration it is greater than one or many
Thus, I hope you will get normally.

We had a similar problem while trying to put back-end data into the chart. Since some of the days were missing it didn't look well. Our solution was;
Create a function like this;
public function generateDates(Date $startDate, Date $endDate, $format = 'Y/m/d'): Collection
{
$dates = collect();
$startDate = $startDate->copy();
for ($date = $startDate; $date->lte($endDate); $date->addDay()) {
$dates->put($date->format($format), 0);
}
return $dates;
}
In your case it's going to be (today and today - six days) and you will union returning collection with your query collection. What it does is; it create a date range from the keys and fill them with zero. When your query collection has some value other than zero - it is going to overwrite it.

Related

Laravel avoid overlapping dates

I am new to Laravel and am trying to implement reservation dates that can not overlap on any day.
I have a model called 'Bookings' that includes the room_id, start_date and end_date.
I have validation that checks that the end date can not be before the start:
$this->validate($request, [
'start_date' => 'required|date',
'end_date' => 'required|date|after_or_equal:start_date',
]);
However I am not sure how to check that the date range does not conflict with any other date ranges of the same room_id stored in the bookings table (as one room can not be booked twice in the same range).
Any help would be appreciated,
Thanks!
In a typical overlaping, given two events A and B, you have to consider four scenarios:
Event A contains event B
Event B contains event A
End date of event A overlaps with start date of event B
End date of event B overlaps with start date of event A
So, in sql to check if a room is busy in a interval of dates, lets say 2018-11-20' and '2018-11-30' it would be:
select *
from rooms
where
start_date between '2018-11-20' and '2018-11-30' or
end_date between '2018-11-20' and '2018-11-30' or
'2018-11-20' between start_date and end_date or
'2018-11-30' between start_date and end_date;
In laravel what I do is to create a scope in the model.
public function scopeByBusy($query,$start_date,$end_date)
{
return $query->whereBetween('start_date', [$start_date, $end_date])
->orWhereBetween('end_date', [$start_date, $end_date])
->orWhereRaw('? BETWEEN start_date and end_date', [$start_date])
->orWhereRaw('? BETWEEN start_date and end_date', [$end_date]);
}
Finally, I can verify if the room with the id 95 is busy between '2018-11-20' and '2018-11-30' like this:
$room_id = 95;
$start = '2018-11-20';
$end = '2018-11-30';
$exists = Rooms::where('id', $room_id)
->byBusy($start, $end)
->first();
if($exists)
{
echo "the room is busy in the interval you selected";
}
I hope it helps you.
I had a similar usage where i needed to check if person has already scheduled something in date range
An example laravel code below :
$start = Carbon::parse($request['start_date'])->format('Y-m-d 00:00:00');
$end = Carbon::parse($request['end_date'])->format('Y-m-d 23:59:59');
$existsActive = Microsite::where(function ($query) use ($start) {
$query->where('start_date', '<=', $start);
$query->where('end_date', '>=', $start);
})->orWhere(function ($query) use ($end) {
$query->where('start_date', '<=', $end);
$query->where('end_date', '>=', $end);
})->count();
if($existsActive > 0 ){
echo "Already exists";
}
There's built-in Carbon method isPast() so you can use:
$start_date->isPast()
$end_date->isPast()
You can check If both selected were booked in past.
If you haven't used Carbon this is the link:
https://carbon.nesbot.com/docs/
if(DB::table('rooms')->whereBetween('start_date', [$request->start_date, $request->end_date])
->orwhereBetween('end_date',[$request->start_date, $request->end_date])->exists())
{
echo "the room is busy in the interval you selected";
}
You have to check that in database. So you can do this:
$result = Bookings::where('start_date', '<=' $request->start_date)->where('end_date', '>=' $request->end_date)->where('room_id',$request->room_id)->first();
if(!$result){
// Code to Book room
}

How to customize Laravel WhereBetween if the value is not found

Im using Laravel WhereBetween like this
$from = '2015-01-02';
$to = '2015-01-30';
$users = User::whereBetween('created_at', [$from,$to])->get();
If the date value not found in User model, then the date will not show right?
I still want to show the date, even the date not found then I set value to 0.
Then the output will be like this
[
{'2015-01-02': 201},
{'2015-01-03': 0},
{'2015-01-04': 0},
{'2015-01-05': 7},
...
{'2015-01-30': 0}
]
Thanks.
You need to execute following query at first to get all user created at the given range,
$data = User::select([
DB::raw('DATE(created_at) AS date'),
DB::raw('COUNT(id) AS count'),
])
->whereBetween('created_at', [$from,$to])
->groupBy('date')
->orderBy('date', 'ASC')
->get();
With above query, you will get count of user for each day and this will not give 0 value if no users created at that day.
Now, what you have to do is to change above collection to array and insert 0 values if no users created.
$userCount = $data->toArray();
$dataByDay = array();
foreach($userCount as $count){
$dataByDay[$count->date] = $count->count;
}
Now, insert zero values as:
$dateFrom = Carbon::parse($from)->format('Y-m-d');
$dateTo = Carbon::parse($to)->format('Y-m-d');
$days = $dateTo->diffInDays($dateFrom);
for($i=0; $i<$days; $i++){
$dateString = $dateFrom->format('Y-m-d');
if(!isset($chartDataByDay[ $dateString ] {
$dataByDay[ $dateString ] = 0;
}
}
Now, $dataByDay array gives what you want.
Hope you understand.

Laravel carbon retrieve record even when it shouldn't

I am searching by start and end date or with a weekday, which kind of works fine however if weekday is equal to 'WE' and today's weekday is equal to 'WE' it still brings back results even when $today date is < than endate.
public function show($id)
{
$weekMap = [
0 => 'SU',
1 => 'MO',
2 => 'TU',
3 => 'WE',
4 => 'TH',
5 => 'FR',
6 => 'SA',
];
$todayWeek = Carbon::now()->dayOfWeek;
$today= Carbon::now();
$weekday = $weekMap[$todayWeek];
$event = Event::with('businesses')
->where('startdate', '<', $today->format('Y-m-d'))
->where('endate', '>', $today->format('Y-m-d'))
->orWhere('weekday', '=', $weekday)
->get();
dd($event);
return view('events.showEvent', compact('event'));
}
If I change orWhere to where, results are all good, however user not always enters a weekday so it can be empty. I want it to work like this:
output everything where startdate < today and where endate > today and if weekday is not empty, output everything where startdate < today, endate > today and weekday = $weekday. I hope that makes sense.
//edit
sample data:
data
So what I want is really search by frequency so it would look like this:
if frequency = daily
compare start and end with today's date.
if frequency = weekly
compare start and end with today's date and weekday.
bring back all results for frequency = daily+weekly
you can use if condition in query like this:
$query = Event::with('businesses')
->where('startdate', '<', $today->format('Y-m-d'))
->where('endate', '>', $today->format('Y-m-d'));
if ($weekday != '') {
$query->where('weekday', $weekday);
}
$event = $query->get();
also
condition if ($weekday != '') check $weekday isset or not you can use
if ( ! is_null($weekday )) instead

Laravel 5 - how to get a random row from first 30 records in eloquent?

I am trying to get a random row from the top 30 records in a table. I sort all the records by score first, and take 30 records in a scope of the eloquent model:
public function scopePopular($query, $d)
{
return $query->where('d', $d)->orderBy('score', 'desc')->take(30);
}
Then in a class:
$cnt = Record::popular($d)->count();
if ($cnt == 0)
return;
$randIndex = rand(0, $cnt-1);
$record = Record::popular($d)->skip($randIndex)->take(1)->first();
return $record;
But when I check in php artisan tinker, I found that Record::popular($d)->count(); will return all the records number instead of 30. How can I correct this problem? Thanks.
Use get() before count() to run the query before count:
$cnt = Record::popular($d)->get()->count();
You are running the query 2 times. That is not necessary.
$cnt = Record::popular($d)->count(); // First query
if ($cnt == 0)
return;
$randIndex = rand(0, $cnt-1);
$record = Record::popular($d)->skip($randIndex)->take(1)->first(); // Second query
return $record;
Instead you can do it like this:
return Record::popular($d)->get()->random(); // One query only

Getting daily aggregates/sum sorted by day in Laravel

So getting a sum()/count() is really easy in Laravel...
but how would I look at the past month, and get the sum of rows every day?
EG...grouped by day that they were created at.
So I want to return a count such as 3, 2, 4, 5
Meaning 3 rows were created on todays date, 2 rows yesterday, 4 rows the day before...etc
How to do this in Laravel easily?
When I use the group by created_at it always just returns 1.
Anybody know how to do it?
Thanks
I've provided the same answer on another post. Shortening it:
$date = new DateTime('tomorrow -1 month');
// lists() does not accept raw queries,
// so you have to specify the SELECT clause
$days = Object::select(array(
DB::raw('DATE(`created_at`) as `date`'),
DB::raw('COUNT(*) as `count`')
))
->where('created_at', '>', $date)
->group_by('date')
->order_by('date', 'DESC')
->lists('count', 'date');
// Notice lists returns an associative array with its second and
// optional param as the key, and the first param as the value
foreach ($days as $date => $count) {
print($date . ' - ' . $count);
}

Resources