It looks like you're trying to refactor your code to avoid duplication, which is a good practice. However, the issue you're encountering is that you're not using the return value from your partOfTeam method in your show method. When you call partOfTeam, it's returning a redirect or a view, but you're not doing anything with that return value in show. Instead, show always returns the 'dashboard' view.
To fix this, you should capture the return value from partOfTeam and return it from show if it's not null. Here's how you can modify your show method:
public function show(string $token)
{
$response = $this->partOfTeam($token);
// If partOfTeam returned a response (redirect or view), return it
if ($response) {
return $response;
}
// If partOfTeam did not return a response, then the user is part of the team
return view('dashboard');
}
And your partOfTeam method remains the same:
private function partOfTeam(string $token) {
$team = Team::where('token', '=', $token)->first();
if ($team == null) {
return redirect('/team');
}
if ($team->users()->find(auth()->id()) == null) {
return view('join-team', [
'team' => $team
]);
}
// If the user is part of the team, return null
return null;
}
With this change, show will now properly handle the redirect or view returned by partOfTeam. If partOfTeam returns null, it means the user is part of the team, and the 'dashboard' view should be shown.