DevMarketer / LaraFlash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.
MIT License
64 stars 15 forks source link

Flashes are not persisting across redirects #8

Open axieum opened 7 years ago

axieum commented 7 years ago

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 commented 7 years ago

Fix:

LaraFlash::keep();
jacurtis commented 7 years ago

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.

axieum commented 7 years ago

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.

HafiziNordin commented 6 years ago

any solution for this issues ?

pazitron1 commented 6 years ago

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?

Elmzelji commented 6 years ago

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

Elmzelji commented 6 years ago

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

gorankrgovic commented 6 years ago

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 :)

marcellopato commented 6 years ago

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!

basepack commented 6 years ago

+1

Shane-Bryan commented 6 years ago

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);
}
shahnotes commented 5 years ago

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']
},
Sogl commented 5 years ago

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

Any ideas how to get it working with original functionality?