Saturday, April 15, 2017

Simulate second login and query through relations off that

What ways are there to simulate another login without password (1 user, some playable characters).

It worked when a User created the Team. But it does make more sense that a Character creates/joins a Team. Since a User hasMany Characters, I'm completly screwed. How do I know which of all the Characters creates/joins a Team? It was easy with only 1 User doing this.

Probably missing some general programming skills, but I'm learning!

Relations
User hasMany Characters <> Character belongsTo User
Character belongsTo Team <> Team hasMany Characters

URL
In team.index I really don't get how to collect all Characters of a Team. The url is currently just /team. Do I have to use something like /{character}/{team}? But I like the simplicity of just /team.

Code
I came up with variants of the following, but if feels wrong. And is wrong :-D

// how would the app know which of the Characters.Team we need. App and me don't know...
$team = auth()->user()->characters()->team()->first();
$characters = \App\Character::where('team_id', $team->id)->get();

Clarify this for me, please!

Are the relations wrong?
Is my approach wrong?
Am I missing easy Laravel magic?

Thank you.



via Bensen

Advertisement