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

< GDB >'s avatar

Store route is shown instead of show, until refresh page

Hi,

I'm a bit confused trying to solve a small issue.

So I have my store-method in controller that at the end of the necessary actions redirects to the show view like so :

// Return view
        return redirect()->route('user.profile.show', ['team'=>$team]);

The view I ended up with looked not as expected, until I refreshed the page. So I renamed my routes to find out what was being shown and it appears to be my store-route

So this route :

// Team store
            Route::post('/teamstore', [TeamController::class, 'store'])->name('team.store');

instead of :

// Profile show
            Route::get('/myprofile', [UserController::class, 'show'])->name('user.profile.show');

So what happens is it shows kinda the right page, but some data is missing. (url = /teamstore) When I refresh the page, I get this :

The page that you're looking for used information that you entered. Returning to that page might cause any action that you took to be repeated. Do you want to continue?

Doing this results in the right view with correct data.

Can anyone guide me in the right direction to what I've to look for? How this type of 'error' can occur?

As always help is appreciated and thank you in advance!

0 likes
10 replies
automica's avatar

@< gdb >

if you are renaming routes, you need to clear the cache afterwards.

can you show the whole method that has:

 // Return view
        return redirect()->route('user.profile.show', ['team'=>$team]);

at the bottom of it?

with your user.profile.show route, you are passing a team argument but in your route:

  Route::get('/myprofile', [UserController::class, 'show'])->name('user.profile.show');

you aren't using the argument.

if you are getting your user from Auth, why are you passing team to it?

1 like
< GDB >'s avatar

hey @automica ,

Yes, this is the complete store method :

// Store
    public function store(Request $request){
        
        //Check if user is already part of a team
        if(isset(Auth::user()->team_id)){
            //Return back with error
            return Redirect::back()->with('error', 'user already belongs to a team'); 
        }

        //Fetch web URL
        $teamurl = $request->input('teamurl'); 
            //Tidy up
            if(!preg_match("~^(?:f|ht)tps?://~i", $teamurl)) {
                $teamurl = "https://" . $teamurl;
            }
                //Merge for validation
                $request->merge(['teamurl' => $teamurl]);
        
        //Check if input is unique - else redirect
        if(Team::where('teamurl', '=', $request->input('teamurl))->exists()){
            // Exists -> Redirect to ask to join
            return view('team.join');
        }

        //Validate input
        request()->validate([
            'teamurl' => ['required', 'string', 'active_url', 'unique:teams'],
            'name' => ['required', 'string', 'unique:teams'],
        ]);
    
        //Process
        $team = new Team();
        $team->teamurl = $request->input('teamurl');
        $team->name = $request->input('name');
        
        //Save 
        $team->save();
        
        //Link Team to User
        Auth::user()->update(['team_id' => $team->id]);

        //Return view
        return view('user.profile.show', ['team'=>$team]);    
    }

Regarding the route, yes my route :

 Route::get('/myprofile', [UserController::class, 'show'])->name('user.profile.show');

Does not contain any team argument, as it was not necessary. But as I got further in my 'project' I needed to give the argument with the route in order for it to function correctly (otherwise $team is undefined error)

automica's avatar

don't return the

user.profile.show

view

redirect to the show method

return redirect('user.profile.show')

I guess that show method has a $team being passed from the method into the view.

1 like
< GDB >'s avatar

@automica

hmm, I tried what you suggested as in :

return redirect('user.profile.show')

This results in the error :

Undefined variable: team

It points to f.ex. the following line

<img    src="{{ asset($team->avatar) }}"

This is from an include (teamProfile) in the show-view. (btw it does do the store logic and row is added to database)

This is also why I added the 'team' arguments to my route. So.... if I try to add that logic to your suggestion, I get the error :

Argument 2 passed to Symfony\Component\HttpFoundation\RedirectResponse::__construct() must be of the type int, array given, called in 

In this case the store-method also function but the error pops up, if i refresh

very similar as original issue 

"The page that you're looking for used information that you entered. Returning to that page might cause any action that you took to be repeated. Do you want to continue?"

I continue -> correct view is shown (so almost identical as original issue, just now an error is thrown)

automica's avatar

what does your UserController::show method look like?

surely you are passing $team into the blade from the controller?

1 like
< GDB >'s avatar

@automica

This is my UserController::show

public function show(){
//Check if User has Team
        if(!empty(Auth::User()->team_id)){
            $team = Team::where('id', Auth::User()->team_id)->firstOrFail();
            return view('user.profile.show', ['team'=>$team]);
        } //Else
        return view ('user.profile.setup');
    }

Regarding :

surely you are passing $team into the blade from the controller?

Yeah, only if the user does have a team though

automica's avatar
automica
Best Answer
Level 54

so hitting:

/myprofile

will always return a view with either a team set up or a means for you to add a team.

if you don't have a team, you add one, then save it to:

 Route::post('/teamstore', [TeamController::class, 'store'])->name('team.store');

and when you are finished, you've added a team and then you redirect to

/myprofile.

perhaps your redirect isn't working?

try:

 return redirect()->route('user.profile.show');
1 like
< GDB >'s avatar

@automica

Answering your question :

/myprofile
  • If User is not yet part of a team (default basically), the /myprofile will load with an include 'add_team' that will give possibility to user to add team (if team is already created he'll get a redirect to join the team)

    if you don't have a team, you add one, then save it to:

  • Correct, the User fills in the form (just 2 fields) from the include 'add_team' which hits the TeamController-store-method.

    and when you are finished, you've added a team and then you redirect to

  • Correct, basically same as step one, only show controller detects that a team is linked and will show a form with the 2 fields filled in (+ several other fields that can be edited and updated) -> works fine

    perhaps your redirect isn't working?

Hèhè, I tried with

 return redirect()->route('user.profile.show');

and

 return redirect()->route('user.profile.show', ['team'=>$team]);

Both are working... so..... inevitable question....

How come my redirect wasn't working?

And 1 thing I'm unsure off is

why was a redirect is necessary instead of just return view('...

EDIT : OK so my first question to you 'why it failed'. It's because I'm redirected to a named route right? https://laravel.com/docs/8.x/redirects#redirecting-named-routes

If I wanted to use like so :

return redirect('user.profile.show')

I would have to specify that in my web.php routes, right?

Please tell me I'm right 😃

automica's avatar

@< gdb >

How come my redirect wasn't working?

I think I gave you an incorrect bit of code for your redirect which was:

return redirect('user.profile.show')

should have been:

 return redirect()->route('user.profile.show');

why was a redirect is necessary instead of just return view('...

because you should never return a view for a POST method. POST / PUT deals with storing data.

Save your data and then redirect to a GET method to display it.

A GET should be idempotent, meaning you can continue to call the same route without it changing your resource.

A bit of reading for you regarding this: https://www.restapitutorial.com/lessons/idempotency.html

1 like
< GDB >'s avatar

because you should never return a view for a POST method. POST / PUT deals with storing data. Save your data and then redirect to a GET method to display it.

Very clear feedback!! I did not catch / realise that!

After giving it a quick thought, yes I think this :

return redirect('user.profile.show')

Can be :

return redirect()->route('user.profile.show'); //specify route name > in steps

return redirect(route('user.profile.show')); // specify route name > joined

return redirect('/myprofile'); // specify route


I'll have to test my 2 additions though, so for anyone else reading this, look at best answer to be sure! 

Thank you very much @automica !! Not only is my problem solved but I learned (I hope 😇) what the culprit was, how to solve it and a bit of homework to get more acquainted with http redirects 💪

The beginning of that idempotency video was spot on 🤣

1 like

Please or to participate in this conversation.