Foreach collection on every passed week
I have a database with users, and one with entries called “gubbe”. I want to have a datatable with all weeks that were included in the search with values in it set to true or false depending on the input in the database called “gubbe”. Like so:
I need some help here. So far, I’ve only been able to do it if it is just one person, adding my code on how i got it working for the 1 user here below.
Basically I have one collection with users. The users have a hasMany relation to gubbe which is connected through ID. I also have a collection with weeks, filtered on the same dates as the users collection.
I basically want to foreach weeks, and then have it shown as the image. I just cant figure out how or where to do the merges / joins.
How i have it set for one user:
public function teamShow($id) {
if (Auth::user()->id == $id or Auth::user()->permission >= 4) {
$gubbar = Gubbe::whereUserId($id)->orderBy('created_at', 'desc')->get();
$first = Gubbe::whereUserId($id)->orderBy('created_at', 'asc')->first();
if (count($first) == 0) {
$first = Gubbe::orderBy('created_at', 'asc')->first();
}
$week_date = new DateTime($first->created_at);
$week = $week_date->format('W');
$year = $week_date->format('Y');
$p = new DatePeriod(
new DateTime($week_date->setISODate($year,$week)->format('Y-m-d')),
new DateInterval('P1W'),
new DateTime()
);
$user = user::find($id);
$weeks = collect([]);
foreach ($p as $w) {
$test = gubbe::whereWeek($w->format('Y') . '-W' . $w->format('W'))->whereUserId($id)->get();
if (count($test) == 0) {
$weeks->push($w->format('Y') . '-W' . $w->format('W'));
}
}
$merged = $weeks->merge($user->gubbe);
dd($merged, $user->gubbe);
//$weeks1 = week::whereBetween('weeknr', [$first->week, $year . '-W' . $week])->whereHas('gubbe', function($q) use ($id){$q->whereUserId($id);})->get();
$vars['merged'] = $merged;
$vars['gubbar'] = $gubbar;
$vars['user'] = $user;
$vars['weekExists'] = '';
$vars['url'] = 'user';
return view('other.gubbe_statistik', $vars);
} else {
$user = User::whereId($id)->first();
Message::error('Du har inget tillstånd att se personsidan för ' . $user->name);
return redirect('/user/' . Auth::user()->id);
}
}
Any ideas on how to make it work for several like I show it?
from Laravel Questions and Answers https://laravelquestions.com/php/foreach-collection-on-every-passed-week/
via Lzo Media
No comments:
Post a Comment