1

Obviously this does not exist but I am looking for a way to implement it.

Example, I am playing a game where I have 3 teams but I can only be actively playing for one team at any time and when I switch team I essentially starting from the beginning but I would like to retain the progress if I switch team.

My tables would look something like this:

Users:

  • active_team_id

TeamProgress:

  • user_id
  • team_id
  • team_level
  • team_xp

TeamNames:

  • id
  • team_name

So there would only be 3 teams defined in TeamNames and while any one of these 3 are active (as set in active_team_id in the users table) then I want to be able to directly target it with something like

$user->teamProgress->update()

and update the active team directly.

Similarly I would like to do something like

$user->teamProgress->get()

and just get the team progress for that users active team while a few others may exist within team progress.

Then if I switch the active_team_id on the user table and do the same calls above I now update / get info for the second team specified in active_team_id

Is there a way to do this with a relation or am I overthinking this and better off doing this by just directly targeting the TeamProgress model using the information I already have as a user?

$teamProgress->where('user_id', $user->id)->where('team_id', $user->active_team_id)->get()

1 Answer 1

1

You can try this package https://github.com/topclaudy/compoships. It allows you to define relations by matching multiple columns. For example

class User extends Model
{
    use \Awobaz\Compoships\Compoships;
    
    public function activeTeamProgress()
    {
        return $this->hasOne(TeamProgress::class, ['id', 'active_team_id'], ['user_id', 'team_id']);
    }
}
2
  • 1
    I think this might be exactly what I am looking for! Although I was hoping to keep it within Laravel haha...thanks! Commented Nov 23, 2022 at 21:37
  • @MatthewJonat it's up to you. I think you can just add methods for getting and updating active progress to the User model. Commented Nov 23, 2022 at 22:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.