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

prince69's avatar

How to return view with success message & data?

I want to return view with success message and data. I tried this way

 return redirect()->route('some.route')->with(compact('data'))
            ->with('success', 'thank you');

I got success message in that view but variable is not passed. It shows an error "Undefined variable"

May be I tried with wrong way. What is the proper way to pass variable data with success message in return view??

0 likes
3 replies
iamamirsalehi's avatar

@prince69 you can try this one


return view('path.to.view', compact('data'))->with('success', 'thank you');

Snapey's avatar

you cannot pass data via a redirect. You either have to include it in the url or pass via session using flash

There are many packages for this such as laracasts own flash helper

https://github.com/laracasts/flash

Please or to participate in this conversation.