G.Rose
G.Rose

Reputation: 714

Running Javascript in Laravel 5.7 event listener

I am trying to run a javascript library, Noty, upon user login. I've already registered the event and followed the documentation here. However I tried to use echo to send down the javascript, but the code isn't executing from what I can see. The workflow is that upon login in the Home page a noty notification will pop up saying welcome.

Here is my EventServiceProvider:

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'Illuminate\Auth\Events\Login' => ['App\Listeners\UserEventListener@onUserLogin']
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();


    }
}

UserEventListener:

class UserEventListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Login  $event
     * @return void
     */
    public function handle(Login $event)
    {

    }

    public function onUserLogin($event) {
         echo("<script>
            $(document).ready(function() {
                    new Noty({
                    text: 'Welcome {{Auth::user()->name}}! 😁',
                    type:'info',
                    layout: 'centerRight',
                    timeout:2000
                })
                .on('onShow', function() {
                        var audio = new Audio('/sounds/appointed.mp3');
                        audio.play();
                    }).show();
            });
        </script>");
        echo("adad");
    }
}

Upvotes: 2

Views: 3324

Answers (2)

Whoami
Whoami

Reputation: 326

It seems to be a very strange approach for displaying messages with Laravel.

You could simply use this line In you login controller

return redirect('your/path')->with('success', 'your message here');

While somewhere at your blade template perform simple check:

@if (\Session::has('success'))
    <script>
            $(document).ready(function() {
                    new Noty({
                    text: 'Welcome {{ Auth::user()->name }}',
                    type:'info',
                    layout: 'centerRight',
                    timeout:2000
                })
                .on('onShow', function() {
                        var audio = new Audio('/sounds/appointed.mp3');
                        audio.play();
                    }).show();
            });
        </script>
@endif

You can put your notification code in common(for ex. layout) template so if there is a message in user's session it will fire notification anywhere

Upvotes: 2

djug
djug

Reputation: 550

what you are trying to do doesn't seem possible, since the listener is not going to render any HTML page (where to code would be executed) but rather it will just execute some PHP code.

Upvotes: 0

Related Questions