Level 18
What you want to do is check in the controller if this user owns the team, then if so get all team member's IDs and use whereIn. This is assuming you only store user_id on the Timesheets table (not team_id).
public function index()
{
$user = auth()->user();
$team = $user->currentTeam;
$userids = [$user->id];
// Check if the user owns the team or not?
if ($user->ownsTeam($team)) {
$userids = $team->users->pluck('ids');
}
$currentweektotal = \App\Timesheets::whereIn('user_id', $userids)
->whereBetween('date', [
Carbon::now()->startOfWeek(),
Carbon::now()->endOfWeek()
])
->sum('total');
$previousweektotal = \App\Timesheets::whereIn('user_id', , $userids)
->whereBetween('date', [
Carbon::now()->subDays(7)->startOfWeek(),
Carbon::now()->subDays(7)->endOfWeek()
])
->sum('total');
$previousweekcount = \App\Timesheets::whereIn('user_id', , $userids)
->whereBetween('date', [
Carbon::now()->subDays(7)->startOfWeek(),
Carbon::now()->subDays(7)->endOfWeek()
])
->count('total');
return view('Timesheets.index', compact('currentweektotal', 'previousweektotal', 'previousweekcount'));
}