Code Monkey home page Code Monkey logo

Comments (13)

HafiziNordin avatar HafiziNordin commented on July 29, 2024 5

any solution for this issues ?

from laraflash.

shahnotes avatar shahnotes commented on July 29, 2024 3

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']
},

from laraflash.

jacurtis avatar jacurtis commented on July 29, 2024 1

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.

from laraflash.

gorankrgovic avatar gorankrgovic commented on July 29, 2024 1

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

from laraflash.

marcellopato avatar marcellopato commented on July 29, 2024 1

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!

from laraflash.

axieum avatar axieum commented on July 29, 2024

Fix:

LaraFlash::keep();

from laraflash.

axieum avatar axieum commented on July 29, 2024

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.

from laraflash.

pazitron1 avatar pazitron1 commented on July 29, 2024

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?

from laraflash.

Elmzelji avatar Elmzelji commented on July 29, 2024

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

from laraflash.

Elmzelji avatar Elmzelji commented on July 29, 2024

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

from laraflash.

basepack avatar basepack commented on July 29, 2024

+1

from laraflash.

Shane-Bryan avatar Shane-Bryan commented on July 29, 2024

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);
}

from laraflash.

Sogl avatar Sogl commented on July 29, 2024

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

Any ideas how to get it working with original functionality?

from laraflash.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.