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

muuucho's avatar
Level 11

Multi tenant question again

I have this tables with columns in my multi tenant app where a user can switch between many teams:

users: id, email, team_id

teams:id, name

team_user; id, team_id, user_id, username

I have this Models:

User

public function team()
    {
        return $this->belongsTo(Team::class);
    }
public function teams() 
    {
        return $this->belongsToMany(Team::class);
    }

User

public function users()
    {
        return $this->belongsToMany(User::class); 
    }

TeamUser

 public function team()
    {
        return $this->belongsTo(Team::class);
    }
    public function user(){
        return $this->belongsTo(User::class);
    }

On every loggged in page, I want to be able to, in a smooth way, get the following data for the logged in user: users.email, teams.name, and team_user.username. As you can see, this data comes from all 3 tables above.

Maybe something like: $user = User::with('currentTeam', 'currentTeamUser)->find(Auth::id())

But how can I achieve that?

0 likes
4 replies
LaryAI's avatar
Level 58

To achieve the desired functionality of retrieving the users.email, teams.name, and team_user.username for the logged-in user, you can utilize Eloquent relationships and eager loading in Laravel. Here's how you can set it up:

Step 1: Define Relationships in Models

First, ensure that your models have the correct relationships defined. Based on your description, you need to adjust the relationships slightly.

User Model

class User extends Model
{
    public function currentTeam()
    {
        return $this->belongsTo(Team::class, 'team_id');
    }

    public function teams()
    {
        return $this->belongsToMany(Team::class, 'team_user');
    }

    public function teamUsers()
    {
        return $this->hasMany(TeamUser::class);
    }
}

Team Model

class Team extends Model
{
    public function users()
    {
        return $this->belongsToMany(User::class, 'team_user');
    }
}

TeamUser Model

class TeamUser extends Model
{
    public function team()
    {
        return $this->belongsTo(Team::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

Step 2: Retrieve Data with Eager Loading

Now, you can retrieve the data using eager loading. Assuming you want to get the current team and the username from the team_user table for the logged-in user, you can do the following:

use Illuminate\Support\Facades\Auth;

$user = User::with(['currentTeam', 'teamUsers' => function($query) {
    $query->where('team_id', Auth::user()->team_id);
}])->find(Auth::id());

$email = $user->email;
$teamName = $user->currentTeam ? $user->currentTeam->name : null;
$username = $user->teamUsers->first() ? $user->teamUsers->first()->username : null;

return compact('email', 'teamName', 'username');

Explanation

  • Eager Loading: The with method is used to eager load the currentTeam and teamUsers relationships.
  • Filtering teamUsers: The teamUsers relationship is filtered to only include the record for the current team of the user.
  • Accessing Data: We access the email directly from the User model, name from the currentTeam, and username from the first teamUser record.

This setup should give you the required data in an efficient manner. Adjust the logic as needed based on your application's specific requirements.

muuucho's avatar
Level 11

I "gave up" making it fancy with relations in models and also, I skipped Eloquent, and ended up having this in a Service class:

public function getUser(){
      return DB::table('users')
          ->join('teams', 'users.team_id', '=', 'teams.id')
          ->join('team_user', 'team_user.user_id', '=', 'users.id')
          ->select('email', 'teams.name as teamName', 'team_user.name as userName', 'team_user.is_admin')
          ->where('users.id', Auth::user()->id)
          ->first();
    	}

However, I am greatful for any other ideas.

Please or to participate in this conversation.