synergitech/laravel-sweetalert

This package is abandoned and no longer maintained. The author suggests using the https://github.com/uxweb/sweet-alert package instead.

A PHP package to show Sweet Alerts with the Laravel Framework

0.0.2 2018-07-18 12:30 UTC

This package is auto-updated.

Last update: 2022-02-01 13:13:43 UTC


README

please note this package has been abandoned, please use the original package for identical functionality or our replacement package for more flexible functionality

This package integrates Twig with the standard Laravel 5 view framework. The package is based on Easy Sweet Alert Messages for Laravel by Uziel Bueno but has been modified and refactored for our requirements.

Install

First, install the package using Composer:

composer require synergitech/sweetalert

Please note that this package works only alongside SweetAlert.

Usage

Facade

First, import the Alert facade into your controller.

use Alert;

Then, use the facade methods to add your message.

  • Alert::message('Message', 'Optional Title');
  • Alert::basic('Basic Message', 'Mandatory Title');
  • Alert::info('Info Message', 'Optional Title');
  • Alert::success('Success Message', 'Optional Title');
  • Alert::error('Error Message', 'Optional Title');
  • Alert::warning('Warning Message', 'Optional Title');

Helper Function

The helper supports the same methods as the facade.

  • alert()->message('Message', 'Optional Title');
  • alert()->basic('Basic Message', 'Mandatory Title');
  • alert()->info('Info Message', 'Optional Title');
  • alert()->success('Success Message', 'Optional Title');
  • alert()->error('Error Message', 'Optional Title');
  • alert()->warning('Warning Message', 'Optional Title');
  • alert()->basic('Basic Message', 'Mandatory Title')->autoclose(3500);

For a generic alert, you can just use alert('Message'), which has the same outcome as alert()->message('Message').

Rendering

Blade

@if (Session::has('sweetalert.alert'))
    <script>
        swal({!! Session::get('sweetalert.alert') !!});
    </script>
@endif

Twig

{% if session_has('sweetalert.alert') %}
    <script>
        swal({{ session_get('sweetalert.alert')|raw }});
    </script>
{% endif %}

The sweetalert.alert session key contains a JSON configuration object which can be passed directly to SweetAlert.

Advanced Rendering

Blade

@if (Session::has('sweetalert.alert'))
    <script>
        swal({
            text: "{!! Session::get('sweetalert.text') !!}",
            title: "{!! Session::get('sweetalert.title') !!}",
            timer: {!! Session::get('sweetalert.timer') !!},
            type: "{!! Session::get('sweetalert.type') !!}",
            showConfirmButton: "{!! Session::get('sweetalert.showConfirmButton') !!}",
            confirmButtonText: "{!! Session::get('sweetalert.confirmButtonText') !!}",
            confirmButtonColor: "#AEDEF4"
        });
    </script>
@endif

Twig

{% if session_has('sweetalert.alert') %}
    <script>
        swal({
            text: "{{ Session::get('sweetalert.text')|raw }}",
            title: "{{ Session::get('sweetalert.title')|raw }}",
            timer: {{ Session::get('sweetalert.timer')|raw }},
            type: "{{ Session::get('sweetalert.type')|raw }}",
            showConfirmButton: "{{ Session::get('sweetalert.showConfirmButton')|raw }}",
            confirmButtonText: "{{ Session::get('sweetalert.confirmButtonText')|raw }}",
            confirmButtonColor: "#AEDEF4"
        });
    </script>
{% endif %}