Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sanch012's avatar

If Users owns a team show all team members data

Hi,

I have a simple timesheet application using spark teams where users enter timesheets. Currently a users can view their own timesheets.

I would like the team owner to view all team members timesheet in a view. '

I have found this code in the Spark docs but not sure how to use it in my situations.

if ($user->ownsTeam($team)) {
    // This user owns the team...
}

My controller.

    public function index()
    {
        $currentweektotal = \App\Timesheets::where('user_id', Auth::user()->id)
        ->whereBetween('date', [Carbon::now()->startOfWeek(),Carbon::now()->endOfWeek()])
        ->sum('total');

        $previousweektotal = \App\Timesheets::where('user_id', Auth::user()->id)
        ->whereBetween('date', [Carbon::now()->subDays(7)->startOfWeek(),Carbon::now()->subDays(7)->endOfWeek()])
        ->sum('total');

        $previousweekcount = \App\Timesheets::where('user_id', Auth::user()->id)
        ->whereBetween('date', [Carbon::now()->subDays(7)->startOfWeek(),Carbon::now()->subDays(7)->endOfWeek()])
        ->count('total');
                           

         return view('Timesheets.index', compact('currentweektotal', 'previousweektotal', 'previousweekcount'));
    }
0 likes
1 reply
Braunson's avatar

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'));
}

Please or to participate in this conversation.