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

AdamVaclav's avatar

Call to method from another method inside class

I have this method inside TeamController class that checks is team exists based on url token(if not -> redirect), then checks is user is part of the team(if not -> return join-team view) and then return dashboard if none of the conditions before were met. (I'm sure there are better ways to achieve this but I'm newbie, bear with me please.)

public function show(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
        ]);
    }

    return view('dashboard');

}

This code works as I intended, but I would like to use the conditions in another method and I didn't want to have duplicate code, so I tried to put it inside another private method which I will call to. But for some reason, if team does not exist and I expect redirect, the redirect won't happen, instead 'dashboard' view is shown. Same thing happens if team exists that user is not part of, dashboard is shown instead of join-team.

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
        ]);
    }
}

public function show(string $token)
{

    $this->partOfTeam($token);

    return view('dashboard');

}

Can someone guide me, what I am doing wrong? I'm quite new to OOP so I don't even know how to google my mistake(I have been trying last two hours...)

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

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.

1 like
dualklip's avatar

I think a good approach might be to create a controller following Laravel's modus operandi, for example by doing php artisan make:controller TeamController --model=Team

This will already auto-generate a method like this:

/**
     * Display the specified resource.
     */
    public function show(Team $team)
    {
        //
    }

If you want to customise the field that will be used to find the Team, you can implement this method in the model:

/**
 * Get the route key for the model.
 */
public function getRouteKeyName(): string
{
    return 'token';
}

https://laravel.com/docs/11.x/routing#customizing-the-key

To manage the situation where the Team is not found you have several option, but for example you can use the Fallback method of Route in route/web.php https://laravel.com/docs/11.x/routing#fallback-routes

Or you can use the missing method like this: https://laravel.com/docs/11.x/routing#customizing-missing-model-behavior

Please or to participate in this conversation.