Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uxweb's avatar
Level 20

Sweet Alert Messages for You!

Hi guys!

I just released a package named uxweb/sweet-alert.

This package will let you show beautiful alert messages to your Laravel app users.

The ones that are displayed here on Laracasts when you create or update a new conversation.

If you are using the laracasts/flash, you already are in good shape to use this package.

Note: this is not a replacement for laracasts/flash and is not intended to use on every possible situation a message should be shown.

The package uses this great library:

http://t4t5.github.io/sweetalert/

Check the repo on Github at:

https://github.com/uxweb/sweet-alert

If you have any doubt please reply :)

Thanks.

0 likes
53 replies
lolotoobo's avatar

Thanx for the package. I include it in my actual project :)

bashy's avatar

Can you string them onto the redirect()?

2 likes
uxweb's avatar
Level 20

@lolotoobo Thanks guys, i had some issues with the psr-4 autoloader in version 1.02 through 1.0.4, if you have any issue please install 1.0.5 version

uxweb's avatar
Level 20

@bashy I think right now it does not do it that way. Can you explain more this?.

Currently this package uses the laravel session to store the message.

bashy's avatar

Like instead of

alert()->success('You have been logged out.', 'Good bye!');

return home();

you can do

return home()->alert()->success('You have been logged out.', 'Good bye!');
jimmck's avatar

Hi @uxweb,

Just trying this out. I did the composer dance. I added the service provider and the facade. In PHPStorm it does not like this.

    \Route::get('menu', function()
    {
        Alert::message('Welcome back!');
        return view('coverage');
    });

It cannot resolve the Alert facade. However it does like:

    \Route::get('menu', function()
    {
        SweetAlert::message('Welcome back!');
        return view('coverage');
    });

I took Alert:: ... straight from your Git Readme section.

But I get no alert box? i am confused by what is expected to happen? I expect an alert message and then my normal view.

Just trying this out. Normally I don't launch alert boxes from the server side code.

Note I published the view to resources/vendor

I am basing my code off your example:

public function store()
{
    Alert::message('Welcome Back!');

    return Redirect::home();
}

Jim

uxweb's avatar
Level 20

Thanks for the feedback @jimmck, did you linked the JS and CSS files from Sweet Alert?

uxweb's avatar
Level 20

@bashy I think i'm not sure how to do that. This is my first package, i'm still a noob, but let me check if it is possible to extend the Redirector with the SweetAlert functionality.

If you have more ideas on how to implement this i'll appreciate your help!

Thank you :)

jimmck's avatar

Hi,

Yes. I think it's inn how I am trying to use your stuff. Here is the view:

<!doctype html>
<html>
    <title>Coverage</title>
    <link rel="stylesheet" href="/assets/css/coverage.css">
    <link rel="stylesheet" href="/assets/css/sweetalert.css">
<body>
    <script src="/assets/js/jquery-1.10.2.js"></script>
    <script src="/assets/js/sweetalert.min.js"></script>
    <!-- Include this after you include the sweet alert js file -->
    @include('sweet::alert')
    @yield('content')
    <script>
        (function() {
            var wrap = $('div#app');
            var melissa;
            //alert("Started...");

            $('a').on('click', function( e ) {
                var href = $(this).attr('href');
                wrap.load( href );
                e.preventDefault();
            });
        })();
    </script>
</body>
</html>

And then then the Route. I am using the Message construct (I guess??) from your store example. I don't see how you are setting up the alert dialog in JS? I am used to doing this on the client side. In my alerts the server sends data and the alert displays it.

The route:

    \Route::get('menu', function()
    {
        SweetAlert::message('Welcome back!');
        return view('coverage');
    });

The coverage view is the blade layout you see.

Jim

jimmck's avatar

@uxweb I am assuming I can do this right in the route for testing and skip the controller? Whats up with the SweetAlert? The facade I registered is Alert? Sorry did not read your code all the way through. Laravel cannot load your service via Alert facade, the lookup fails.

uxweb's avatar
Level 20

@jimmck Just fixed a bug within the view template included. Please do a composer update to get the 1.0.6 version and delete the directory published in resources/views/vendor/sweet-alert.

Just created a new Laravel project to test what you are trying to do, it works fine using the Alert facade in a route closure like this:

Route::get('/alert', function() {
    Alert::message('Hey!');

    return view('app');
});

It works good. When using the Alert facade, do you get any errors? maybe prefixing it with a "" like this:

\Alert::message('Hey!');

Thanks again for your feedback!

jimmck's avatar

@uxweb Cool I will update and let you know! Excellent Customer Service :)

jimmck's avatar

@uxweb Hey it works!!!!! But alas, PHPStorm insists on SweetAlert::message('Your Message Goes Here");

Alert::message gives this Stacktrace:

Great work!!!

How does this work? It has great applications!!!

Jim

jimmck's avatar
\Route::get('menu', function()
    {
        Alert::message('Welcome back!');
        return view('coverage');
    });

Works

I just get a flash message and my menu view together. There is no button to press for SweetAlert message. Is that the expectation?

Lemme know if you want me to try anything.

Jim

uxweb's avatar
Level 20

@jimmck Yep, dismissing the alert after some seconds is the default behavior, but it can be changed.

If you want the alert has a confirm button to close do it like:

Alert::message('Thanks for your help!')->persistent('Close Me!');

This will override the default dismissible alert and will only close if you press the alert button.

What did you do to fix the Alert facade?

Thank you!

jimmck's avatar

Here is your Handy Work in action... !!

jimmck's avatar

I did not do anything. Alert::message does not work. See prev message with stacktrace.

jimmck's avatar

Sorry did not switch back, this works...

    \Route::get('menu', function()
    {
        SweetAlert::message('Welcome back!');
        return view('coverage');
    });
jimmck's avatar

It kinda looks like I could do some weird css/div magic to blend these two views????

uxweb's avatar
Level 20

@jimmck, mmm, it looks pretty strange that the Alert facade is not working.

Did you added it to your aliases section in config/app.php?, like this:

'aliases' => [
        // more default laravel aliases
        'Alert'      => 'UxWeb\SweetAlert\SweetAlert',
],

Thank you!

jimmck's avatar

@uxweb Hey I was looking at the error. FYI I have the HTML stuff loaded as well. From Laravel 4... If that helps.

uxweb's avatar
Level 20

@jimmck Ohh i see, are you using Laravel 4?, if so, that explains why it may be failing.

I did this for Laravel 5, but let me test it in L4, i think i will need to make a new branch to support to L4.

Thank you!

jimmck's avatar

Yes I did...

       'Spatie\Backup\BackupServiceProvider',
        'Spatie\Tail\TailServiceProvider',
        'Kris\LaravelFormBuilder\FormBuilderServiceProvider',
        FintechFab\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,
        'Barryvdh\Debugbar\ServiceProvider',
        'Illuminate\Html\HtmlServiceProvider',
        'UxWeb\SweetAlert\SweetAlertServiceProvider',

        'FormBuilder' => 'Kris\LaravelFormBuilder\Facades\FormBuilder',
        'Form' => 'Illuminate\Html\FormFacade',
        'Debugbar' => 'Barryvdh\Debugbar\Facade',
        'HTML' => 'Illuminate\Html\HtmlFacade',
        'Alert' => 'Uxweb\SweetAlert\SweetAlert',
jimmck's avatar

No I am Laravel 5.1.7. I load the HTML stuff separate.

uxweb's avatar
Level 20

@jimmck I think there is a typo in the Facade namespace in your app config file, change Uxweb for UxWeb and give it a try.

Thank you!

jimmck's avatar
Jamess-MacBook-Pro:laravel5 jimm$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing psy/psysh (v0.4.4)
  - Installing psy/psysh (v0.5.1)
    Downloading: 100%

  - Removing laravel/framework (v5.1.6)
  - Installing laravel/framework (v5.1.7)
    Downloading: 100%

  - Removing uxweb/sweet-alert (1.0.5)
  - Installing uxweb/sweet-alert (1.0.6)
    Downloading: 100%

Writing lock file
Generating autoload files
Generating optimized class loader
Jamess-MacBook-Pro:laravel5 jimm$ php artisan vendor:publish
Copied Directory [/vendor/kris/laravel-form-builder/src/views] To [/resources/views/vendor/laravel-form-builder]
Copied Directory [/vendor/uxweb/sweet-alert/src/views] To [/resources/views/vendor/sweet]
Publishing complete for tag []!
Jamess-MacBook-Pro:laravel5 jimm$
uxweb's avatar
Level 20

@jimmck Thanks for share that composer output.

Then if you are using L5 i think the only thing that makes the facade fails is that namespace typo in the aliases list.

uxweb's avatar
Level 20

@jimmck My bad, i have that typo in documentation, i'm improving it and fixing some other typos.

I appreciate your help A LOT!.

jimmck's avatar

No I made the change, does not work. I cut/pasted Facade entry from your GIT Readme you will need to update.

jimmck's avatar

@uxweb Hi, I never wrote a Facade class but compare yours to the HTML Facade class

<?php

namespace UxWeb\SweetAlert;

use Illuminate\Support\Facades\Facade;

class SweetAlert extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'uxweb.sweet-alert';
    }
}

and HTML's

<?php namespace Illuminate\Html;

use Illuminate\Support\Facades\Facade;

/**
 * @see \Illuminate\Html\HtmlBuilder
 */
class HtmlFacade extends Facade {

    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'html'; }

}

Next

Please or to participate in this conversation.