0

I want to put an alert message on my Listener:

if ($type == '-'){
    if ($balance < $amo) {
        flash()->overlay('Warning!', 'Amount is higher than Balance.', 'warning');
    } else {
        $wallet->users()->updateExistingPivot($uid, ["balance" => ($balance - $amo)]);
    }
} else {
    $wallet->users()->updateExistingPivot($uid, ["balance" => ($balance + $amo)]);
}

But this flash() does not seem to be working. I mean, it does not show the message Amount is higher than Balance..

So how to print this message properly via the Listener?

Sebastian Richner
  • 782
  • 1
  • 13
  • 21
  • You should specify the language and the framework you're using. With the information you gave, is unlikely people will be able to help you out. You should mind your audience. – Mario Souza Jul 19 '21 at 15:31

1 Answers1

0

Event should be "sideeffect" of your primary action.

So default meaning is, that event should not have direct response.

I think you should rethink your code design.


ANYWAY

  1. You can do that by putting message into session via your event. This is strongly unrecommended (at least by me). But at least, take care of deleting this message from session after usage.

  2. You have your event emitter in try/catch. If try fits in, you will flash message. If it will not fit in, you will throw an error in your listener and catch it. There you can flash another message. Again, I do not recommend even this solution


EXAMPLE

if($type == '-'){
            if($balance < $amo)
            {
                flash()->overlay('Warning!', 'Amount is higher than Balance.', 'warning');
            }
            else
            {
                $wallet->users()->updateExistingPivot($uid, ["balance" => ($balance - $amo)]);
                throw new \Exception('case1');
            }
        }else{
            $wallet->users()->updateExistingPivot($uid, ["balance" => ($balance + $amo)]);
            throw new \Exception('case2');
        }

And run it like this:

try {
    event(new YourEventEmitter($params));
    return view('dashboard')->with('message', 'event is ok');
} catch (\Exception $e) {
    if ($e->message === 'case1')
        return view('dashboard')->with('message', 'event error 1');
    else if ($e->message === 'case2')
        return view('dashboard')->with('message', 'event error 2');
}

**I DO NOT RECOMMEND THIS!!! IT IS NON-SENSE. EVENT SHOULD NOT DO ANYTHING WITH YOUR RESPONSE.**
Adrian Zavis
  • 233
  • 2
  • 11