So, I've hit a roadblock. I hope I can explain it well.
I'm creating a custom weekly calendar to display 'tasks' for the given week - Sun to Saturday.
I have the following code which is grabbing all tasks within a specified data range (works as it should).
However, I want to display the 7 panels on my view each representing the day of the week. I'll do this in my @foreach on the view itself (which I haven't tackled yet).
Problem is, if there's only 2 task for the week, say on Sunday and Tuesday, I won't be able to create empty panels on my view for the remaining days of the week that won't have any tasks.
How can I ensure all the week's dates are included in the array - even if they don't have a task assigned to them? Or is there a better way?
In Controller:
$weekly_tasks = Auth::user()->tasks()->withinDateRange($beginning_of_week, $end_of_week)->get();
foreach ($weekly_tasks as $task) {
$date = new Carbon($task['date']);
$days[$date->toFormattedDateString()][] = $task;
}
return $days;
Scope on Model:
public function scopeWithinDateRange($query, $start, $end)
{
return $query->whereBetween('date', [$start->subDays(1), $end]);
}
$days array returns something like this FYI:
{
"Mar 22, 2015": [
{
"id": 1,
"task": "stuff",
"date": "2015-03-22 23:06:23"
}
],
"Mar 24, 2015": [
{
"id": 2,
"task": "more stuff",
"date": "2015-03-22 23:06:23"
}
],
}
Array should return something like this:
{
"Mar 22, 2015": [
{
"id": 1,
"task": "stuff",
"date": "2015-03-22 23:06:23"
}
],
"Mar 23, 2015": [
],
"Mar 24, 2015": [
{
"id": 2,
"task": "more stuff",
"date": "2015-03-22 23:06:23"
}
],
"Mar 25, 2015": [
],
"Mar 26, 2015": [
],
"Mar 27, 2015": [
],
"Mar 28, 2015": [
],
}