LaraFlash icon indicating copy to clipboard operation
LaraFlash copied to clipboard

Flashes are not persisting across redirects

Open axieum opened this issue 7 years ago • 13 comments

When calling the following the code, it does not show the LaraFlash data:

LaraFlash::success('Success message...');
return redirect(route('index'));

Hence, the redirect is clearing the flash.

axieum avatar Nov 25 '17 09:11 axieum

Fix:

LaraFlash::keep();

axieum avatar Nov 25 '17 11:11 axieum

Yep. I need to document this feature. But this package uses the same core flash session underneath it as the basic laravel session management. So just like the normal flash sessions in Laravel, flashed data only lasts for 1 request. When you do a redirect within a controller you are actually creating another request which is why you have to use LaraFlash::keep() in order to persist it for another request.

jacurtis avatar Nov 25 '17 16:11 jacurtis

Sorry again, I only closed this issue because I saw your tweet resolving someone else's case.

I am trying to get this working with my implementation, but just can't!

LaraFlash::success('Successfully created a new profile! (' . $profile->name . ')')->keep();
return redirect(route('profile.show', $profile->link));
LaraFlash::success('Successfully created a new profile! (' . $profile->name . ')');
LaraFlash::keep();
return redirect(route('profile.show', $profile->link));

Both code samples above fail to persist the flash through two requests.

axieum avatar Nov 26 '17 05:11 axieum

any solution for this issues ?

HafiziNordin avatar Dec 12 '17 02:12 HafiziNordin

Same problem here. LaraFlash::keep (); does not seem to do anything when dealing with redirections (unless I'm using it wrong). Has anyone managed to successfully use keep () with redieections? Please share how this can be implemented?

pazitron1 avatar Jan 01 '18 22:01 pazitron1

is there any solution ::keep() didn't work for me.

Elmzelji avatar Feb 01 '18 17:02 Elmzelji

it only works if i did return view('') when i redirect nothing happens.

Elmzelji avatar Feb 01 '18 20:02 Elmzelji

Until @jacurtis or anybody address this issue, I propose the following - Using default Laravel flash message. In your method do it as usual with slightly different approach within the message itself:

return redirect()->back()->with('status', 'success|Your message');

Notice that the flash message contains the pipe "|" which basically "divides" the type of the notification and text.

To use this feature then you can create a VueJS component alternating the code @jacurtis provided for the frontend side. I registered the component as toasty-notification. Alternated part looks like this:

props: {
            text: {
                type: String,
                default: null
            }
        },
        data: function() {
            return {
                session: this.text,
                validMethods: ['general', 'primary', 'success', 'info', 'warning', 'danger', 'snackbar']
            }
        },
        mounted: function() {

            let vm = this;

            this.$nextTick(function() {
                if ( vm.session !== '' )
                {
                    let res = vm.session.split('|');

                    if (_.includes(vm.validMethods, res[0].toLowerCase())) {
                        vm[res[0].toLowerCase()](res[1]);
                    } else {
                        vm.general(res[1]);
                    }
                }
            })
        },
// the rest of the methods.

And finally place this component in your blade view like this:

<toasty-notification text="{{ session('status') }}"></toasty-notification>

So this is the quick hack :)

gorankrgovic avatar Feb 03 '18 16:02 gorankrgovic

Common @jacurtis , please man! Work on a fix for its issue! The @gorankrgovic hack is good, but need to be on every single blade view!

marcellopato avatar Feb 22 '18 14:02 marcellopato

+1

basepack avatar Jun 20 '18 05:06 basepack

I've found a pretty easy workaround. If you redirect to an action instead of a route you can place your LaraFlash object it the action code. Create a 'redirected' variable and include it in the request.

Code example for UserController

public function update(Request $request, $id)
{
    return redirect()->action(
      'UserController@show', ['id' => $id, 'redirected' => 'true']
    );
}

public function show(Request $request, $id)
{
  $user = User::findOrFail($id);
  if($request->redirected) LaraFlash::new()->content('User Updated')->title('Success')->type('success');
  return view('manage.users.show')->withUser($user);
}

Shane-Bryan avatar Jun 22 '18 08:06 Shane-Bryan

I've found easiest solution for this LaraFlash on redirected route issue. No need to check the redirected request or action route or add keep() method or no need to make new Vue component.

Make changes in your code with below code. Hope it will solve the issue. But priority or any ordering things won't work. Thanks.

Add this below piece of code above the notification Vue object code written. And then pass this $session variable to notification Vue object data.

@php
  $sessions = array();
  $laraflashes = session()->get('laraflash');
  if (!is_null($laraflashes)) {
    $sessions = $laraflashes->toArray();
  }
  session()->forget('laraflash');
@endphp
data: {
      session: {!!json_encode($sessions)!!},
      validMethods: ['general', 'primary', 'success', 'info', 'warning', 'danger', 'snackbar']
},

shahnotes avatar Feb 03 '19 08:02 shahnotes

@shahnotes I didn't go deep into your solution, but it works! Thx!

Any ideas how to get it working with original functionality?

Sogl avatar Aug 28 '19 00:08 Sogl