Reputation: 5367
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": [
],
}
Upvotes: 0
Views: 243
Reputation: 926
You could initialize the $days array with all the days of the week as keys pointing to empty empty arrays and then append all tasks to the corresponding array.
I have never used Laravel before, but a quick google thought me you can get the Carbon of the start of the week, and add the FormattedDateString as a key. Then get the next day using ->tomorrow() and rinse 'n repeat till you reach the end of the week. There probably is a better method though, so only use this if you're stuck.
EDIT: Assuming ($beginning_of_week, $end_of_week) are Carbon objects:
$days = array();
$iterator = $beginning_of_week;
while ( $iterator->lte($end_of_week)){
$days[$iterator->toFormattedDateString()] = array();
$iterator->addDay();
}
This initialises your $days, follow this with your foreach.
Upvotes: 1
Reputation: 148
Define the week array(you should define this array dynamically) and an empty $weekly_tasks_data array. Then looping through that array copy the data to the new array $weekly_tasks_data, if data exists at all.
$week_arr = array("Mar 22, 2015", "Mar 23, 2015", "Mar 24, 2015", "Mar 25, 2015", "Mar 26, 2015", "Mar 27, 2015", "Mar 28, 2015");
$weekly_tasks_data = array();
foreach($week_arr as $k => $v) {
if(array_key_exists($v, $weekly_tasks))
$weekly_tasks_data[$v] = $weekly_tasks[$v];
else
$weekly_tasks_data[$v] = array();
}
Upvotes: 0