Search code examples
phplaravelflash-message

Laravel - HTTP Session Flash Data dont work


The Laravel documentation:

Sometimes you may wish to store items in the session for the next request. You may do so using the flash method.

$request->session()->flash('status', 'Task was successful!');

my code:

 public function store(StorePost $request)
    {
        $validated = $request->validate();

        $post = new Posts();
        $post->title = $validated['title'];
        $post->content = $validated['content'];

        $post->save();

        $request->session()->flash('status', 'Task was successful!');

        return redirect()->route('posts.show', [$post->id]);
    }

and my IDE vscode throw error looks like this: error in flash

Some help in this error ?


Solution

  • have you include the following namespace

    use Session;
    

    if not use 'Session' namespace

    you can also try another way

    public function store(StorePost $request)
        {
            $validated = $request->validate();
    
            $post = new Posts();
            $post->title = $validated['title'];
            $post->content = $validated['content'];
    
            $post->save();
    
            return redirect()->route('posts.show', [$post->id])->with('status','Task was successful!');
        }
    

    it will create a RedirectResponse instance and flash data to the session in a single, fluent method