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

afoster009's avatar

Get all team members job sites if current user is the owner of the Team

Hello, i am plugging away at my app and i am required to have team owners to be able to edit / update / delete all of the the team members job sites but i am not sure how to build the proper query. I currently have this query to retrieve the sites for the current user

$jobSites =  DB::table('job_sites')->where('user_id', auth()->id())->get();

but i am not sure how to modify it so i can get the job sites for all of the users on a given team if the current user is the owner of the team. I tried looking through the docs but only see conditional logic for team stuff and nothing seemed obvious to me.

Any suggestions would be extremely helpful!! thanks in advance :)

0 likes
4 replies
tisuchi's avatar

Since its an authorization part, you can use Gate at Laravel [1].

Another approach can be establishing relationships between user and job site. Then you just check whether that user is eligible for modify or not by using logic.

  1. https://laravel.com/docs/5.6/authorization#gates
afoster009's avatar

@tisuchi

at the moment i just want to write the query which will get all of the Users on the team Job sites. There is a relationship already between Job Sites and users. Also i am using the teams functionality that is baked into Laravel Spark.

I have never used Gates before

Vilfago's avatar

Hard to say without seeing your models and relations... But something like that could give you an hint.

$jobSites =  DB::table('job_sites')
    ->join('users', 'job_sites.user_id', '=', 'users.id')
    ->join('teams', 'teams.id', '=', 'users.team_id') //if you need column from this table
    ->where('users.team_id', Auth::user()->team_id)
    ->get();

Just add a select() to not have all columns...

Cronix's avatar
Cronix
Best Answer
Level 67

This should do it, or be pretty close... could be shorter but wanted it to be verbose for self-explanation

if (auth()->user()->roleOnCurrentTeam() == 'owner') {
    $teamMembers = auth()->user()->currentTeam->users;
    $memberIds = $teamMembers->pluck('id');

    $jobSites = DB::table('job_sites')->whereIn('user_id', $memberIds)->get();
}
1 like

Please or to participate in this conversation.