{"id":13717837,"url":"https://github.com/uxweb/sweet-alert","last_synced_at":"2026-01-11T16:52:02.921Z","repository":{"id":38683456,"uuid":"38935942","full_name":"uxweb/sweet-alert","owner":"uxweb","description":"A simple PHP package to show SweetAlerts with the Laravel Framework","archived":false,"fork":false,"pushed_at":"2024-07-13T06:51:38.000Z","size":409,"stargazers_count":832,"open_issues_count":18,"forks_count":210,"subscribers_count":30,"default_branch":"master","last_synced_at":"2024-11-30T13:58:36.509Z","etag":null,"topics":["alert","laravel","sweet-alert-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uxweb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-07-11T18:22:10.000Z","updated_at":"2024-11-18T14:16:38.000Z","dependencies_parsed_at":"2024-11-14T06:40:52.409Z","dependency_job_id":null,"html_url":"https://github.com/uxweb/sweet-alert","commit_stats":{"total_commits":110,"total_committers":28,"mean_commits":"3.9285714285714284","dds":0.4636363636363636,"last_synced_commit":"d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uxweb%2Fsweet-alert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uxweb%2Fsweet-alert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uxweb%2Fsweet-alert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uxweb%2Fsweet-alert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uxweb","download_url":"https://codeload.github.com/uxweb/sweet-alert/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252842368,"owners_count":21812655,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alert","laravel","sweet-alert-library"],"created_at":"2024-08-03T00:01:27.888Z","updated_at":"2026-01-11T16:52:02.880Z","avatar_url":"https://github.com/uxweb.png","language":"PHP","funding_links":[],"categories":["PHP","Packages"],"sub_categories":["JavaScript Framework Support"],"readme":"# Easy Sweet Alert Messages for Laravel\n\n![A success alert](demos/1XySJiz.png)\n\n[![Latest Version](https://img.shields.io/github/release/uxweb/sweet-alert.svg?style=flat-square)](https://github.com/uxweb/sweet-alert/releases)\n[![StyleCI](https://styleci.io/repos/38935942/shield)](https://styleci.io/repos/38935942)\n[![Total Downloads](https://img.shields.io/packagist/dt/uxweb/sweet-alert.svg?style=flat-square)](https://packagist.org/packages/uxweb/sweet-alert)\n\n## Installation\n\nRequire the package using Composer.\n\n```bash\ncomposer require uxweb/sweet-alert\n```\n\nIf using laravel \u003c 5.5 include the service provider and alias within `config/app.php`.\n\n```php\n'providers' =\u003e [\n    UxWeb\\SweetAlert\\SweetAlertServiceProvider::class,\n];\n\n'aliases' =\u003e [\n    'Alert' =\u003e UxWeb\\SweetAlert\\SweetAlert::class,\n];\n```\n\n## Installing Frontend Dependency\n\nThis package works only by using the [BEAUTIFUL REPLACEMENT FOR JAVASCRIPT'S \"ALERT\"](https://sweetalert.js.org/).\n\n### Using a CDN\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e\n    \u003c!-- Include this in your blade layout --\u003e\n    \u003cscript src=\"https://unpkg.com/sweetalert/dist/sweetalert.min.js\"\u003e\u003c/script\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    @include('sweet::alert')\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Using Laravel Mix\n\nInstall using Yarn\n\n```bash\nyarn add sweetalert --dev\n```\n\nInstall using NPM\n\n```bash\nnpm install sweetalert --save-dev\n```\n\nRequire sweetalert within your `resources/js/bootstrap.js` file.\n\n```javascript\n// ...\n\nrequire(\"sweetalert\");\n\n// ...\n```\n\nThen make sure to include your scripts in your blade layout. Remove the `defer` attribute if your script tag contains it, `defer` will delay the execution of the script which will cause an error as the `sweet::alert` blade template is rendered first by the browser as html.\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e\n    \u003c!-- Scripts --\u003e\n    \u003cscript src=\"{{ asset('js/app.js') }}\"\u003e\u003c/script\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    @include('sweet::alert')\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\nFinally compile your assets with Mix\n\n```bash\nnpm run dev\n```\n\n## Usage\n\n### Using the Facade\n\nFirst import the SweetAlert facade in your controller.\n\n```php\nuse SweetAlert;\n```\n\nWithin your controllers, before you perform a redirect...\n\n```php\npublic function store()\n{\n    SweetAlert::message('Robots are working!');\n\n    return Redirect::home();\n}\n```\n\nHere are some examples on how you can use the facade:\n\n```php\nSweetAlert::message('Message', 'Optional Title');\n\nSweetAlert::basic('Basic Message', 'Mandatory Title');\n\nSweetAlert::info('Info Message', 'Optional Title');\n\nSweetAlert::success('Success Message', 'Optional Title');\n\nSweetAlert::error('Error Message', 'Optional Title');\n\nSweetAlert::warning('Warning Message', 'Optional Title');\n```\n\n### Using the helper function\n\n`alert($message = null, $title = '')`\n\nIn addition to the previous listed methods you can also just use the helper\nfunction without specifying any message type. Doing so is similar to:\n\n`alert()-\u003emessage('Message', 'Optional Title')`\n\nLike with the Facade we can use the helper with the same methods:\n\n```php\nalert()-\u003emessage('Message', 'Optional Title');\n\nalert()-\u003ebasic('Basic Message', 'Mandatory Title');\n\nalert()-\u003einfo('Info Message', 'Optional Title');\n\nalert()-\u003esuccess('Success Message', 'Optional Title');\n\nalert()-\u003eerror('Error Message', 'Optional Title');\n\nalert()-\u003ewarning('Warning Message', 'Optional Title');\n\nalert()-\u003ebasic('Basic Message', 'Mandatory Title')-\u003eautoclose(3500);\n\nalert()-\u003eerror('Error Message', 'Optional Title')-\u003epersistent('Close');\n```\n\nWithin your controllers, before you perform a redirect...\n\n```php\n/**\n * Destroy the user's session (logout).\n *\n * @return Response\n */\npublic function destroy()\n{\n    Auth::logout();\n\n    alert()-\u003esuccess('You have been logged out.', 'Good bye!');\n\n    return home();\n}\n```\n\nFor a general information alert, just do: `alert('Some message');` (same as `alert()-\u003emessage('Some message');`).\n\n### Using the Middleware\n\n#### Middleware Groups\n\nFirst register the middleware in web middleware groups by simply adding the middleware class `UxWeb\\SweetAlert\\ConvertMessagesIntoSweetAlert::class` into the \\$middlewareGroups of your app/Http/Kernel.php class:\n\n```php\nprotected $middlewareGroups = [\n    'web' =\u003e [\n        \\App\\Http\\Middleware\\EncryptCookies::class,\n        ...\n        \\UxWeb\\SweetAlert\\ConvertMessagesIntoSweetAlert::class,\n    ],\n\n    'api' =\u003e [\n        'throttle:60,1',\n    ],\n];\n```\n\n\u003e Make sure you register the middleware within the 'web' group only.\n\n#### Route Middleware\n\nOr if you would like to assign the middleware to specific routes only, you should add the middleware to `$routeMiddleware` in `app/Http/Kernel.php` file:\n\n```php\nprotected $routeMiddleware = [\n    'auth' =\u003e \\App\\Http\\Middleware\\Authenticate::class,\n    ....\n    'sweetalert' =\u003e \\UxWeb\\SweetAlert\\ConvertMessagesIntoSweetAlert::class,\n];\n```\n\nNext step: within your controllers, set your return message (using `with()`) and send the proper message and proper type.\n\n```PHP\nreturn redirect('dashboard')-\u003ewith('success', 'Profile updated!');\n```\n\nor\n\n```PHP\nreturn redirect()-\u003eback()-\u003ewith('error', 'Profile updated!');\n```\n\n\u003e **NOTE**: When using the middleware it will make an alert to display if it detects any of the following keys flashed into the session: `error`, `success`, `warning`, `info`, `message`, `basic`.\n\n### Final Considerations\n\nBy default, all alerts will dismiss after a sensible default number of seconds.\n\nBut not to worry, if you need to specify a different time you can:\n\n```php\n// -\u003e Remember!, the number is set in milliseconds\nalert('Hello World!')-\u003eautoclose(3000);\n```\n\nAlso, if you need the alert to be persistent on the page until the user dismiss it by pressing the alert confirmation button:\n\n```php\n// -\u003e The text will appear in the button\nalert('Hello World!')-\u003epersistent(\"Close this\");\n```\n\nYou can render html in your message with the html() method like this:\n\n```php\n// -\u003e html will be evaluated\nalert('\u003ca href=\"#\"\u003eClick me\u003c/a\u003e')-\u003ehtml()-\u003epersistent(\"No, thanks\");\n```\n\n## Customize\n\n### Config\n\nIf you need to customize the default configuration options for this package just export the configuration file:\n\n```bash\nphp artisan vendor:publish --provider \"UxWeb\\SweetAlert\\SweetAlertServiceProvider\" --tag=config\n```\n\nA `sweet-alert.php` configuration file will be published to your `config` directory. By now, the only configuration that can be changed is the timer for all autoclose alerts.\n\n### View\n\nIf you need to customize the included alert message view, run:\n\n```bash\nphp artisan vendor:publish --provider \"UxWeb\\SweetAlert\\SweetAlertServiceProvider\" --tag=views\n```\n\nThe package view is located in the `resources/views/vendor/sweet/` directory.\n\nYou can customize this view to fit your needs.\n\n#### Configuration Options\n\nYou have access to the following configuration options to build a custom view:\n\n```php\nSession::get('sweet_alert.text')\nSession::get('sweet_alert.title')\nSession::get('sweet_alert.icon')\nSession::get('sweet_alert.closeOnClickOutside')\nSession::get('sweet_alert.buttons')\nSession::get('sweet_alert.timer')\n```\n\nPlease check the CONFIGURATION section in the [website](https://sweetalert.js.org/docs/#configuration) for all other options available.\n\n### Default View\n\nThe `sweet_alert.alert` session key contains a JSON configuration object to pass it directly to Sweet Alert.\n\n```html\n@if (Session::has('sweet_alert.alert'))\n\u003cscript\u003e\n  swal({!! Session::get('sweet_alert.alert') !!});\n\u003c/script\u003e\n@endif\n```\n\nNote that `{!! !!}` are used to output the json configuration object unescaped, it will not work with `{{ }}` escaped output tags.\n\n### Custom View\n\nThis is an example of how you can customize your view to fit your needs:\n\n```html\n@if (Session::has('sweet_alert.alert'))\n\u003cscript\u003e\n  swal({\n      text: \"{!! Session::get('sweet_alert.text') !!}\",\n      title: \"{!! Session::get('sweet_alert.title') !!}\",\n      timer: {!! Session::get('sweet_alert.timer') !!},\n      icon: \"{!! Session::get('sweet_alert.type') !!}\",\n      buttons: \"{!! Session::get('sweet_alert.buttons') !!}\",\n\n      // more options\n  });\n\u003c/script\u003e\n@endif\n```\n\nNote that you must use `\"\"` (double quotes) to wrap the values except for the timer option.\n\n## Tests\n\nTo run the included test suite:\n\n```bash\nvendor/bin/phpunit\n```\n\n## Demo\n\n```php\nSweetAlert::message('Welcome back!');\n\nreturn Redirect::home();\n```\n\n![A simple alert](demos/4bvuJx9.png)\n\n```php\nSweetAlert::message('Your profile is up to date', 'Wonderful!');\n\nreturn Redirect::home();\n```\n\n![A simple alert with title](demos/GsGOtOq.png)\n\n```php\nSweetAlert::message('Thanks for comment!')-\u003epersistent('Close');\n\nreturn Redirect::home();\n```\n\n![A simple alert with title and button](demos/AnRGDY2.png)\n\n```php\nSweetAlert::info('Email was sent!');\n\nreturn Redirect::home();\n```\n\n![A info alert](demos/DxKh3Yx.png)\n\n```php\nSweetAlert::error('Something went wrong', 'Oops!');\n\nreturn Redirect::home();\n```\n\n![A error alert](demos/pIeTEYz.png)\n\n```php\nSweetAlert::success('Good job!');\n\nreturn Redirect::home();\n```\n\n![A success alert](demos/pQz3ijJ.png)\n\n```php\nSweetAlert::info('Random lorempixel.com : \u003cimg src=\"http://lorempixel.com/150/150/\"\u003e')-\u003ehtml();\n\nreturn Redirect::home();\n```\n\n![HTML in message](demos/x44c12a.png)\n\n```php\nSweetAlert::success('Good job!')-\u003epersistent(\"Close\");\n\nreturn Redirect::home();\n```\n\n![A persistent alert](demos/dj3y95K.png)\n\n## License\n\nSweet Alert for Laravel is open-sourced software licensed under the [MIT license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuxweb%2Fsweet-alert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuxweb%2Fsweet-alert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuxweb%2Fsweet-alert/lists"}