Skip to content

AlertConfig API ​

AlertConfig is an immutable value object that encapsulates the complete configuration for a SweetAlert2 alert. It provides serialization and deserialization methods for session storage and JSON rendering in Blade views.

Import ​

php
use RealRashid\SweetAlert\Support\AlertConfig;

Constructor ​

php
new AlertConfig(array $config = [], string $type = 'config')
ParameterDefaultDescription
$config[]The SweetAlert2 configuration array
$type'config'The alert type: 'config' or 'delete'

Factory Method ​

fromJson(string $json): static ​

Create an AlertConfig instance from a JSON string. This is used when reading alert data back from the session:

php
$config = AlertConfig::fromJson($sessionData);

Accessors ​

MethodReturnDescription
toArray()arrayGet the raw configuration array
get(string $key, mixed $default = null)mixedGet a specific config value
has(string $key)boolCheck if a config key exists
type()stringGet the alert type ('config' or 'delete')

Serialization ​

MethodReturnDescription
toJson()stringSerialize to JSON (includes config and type wrapper)
toSwalConfigJson()stringGet raw config JSON for Swal.fire() (no wrapper)

Type Check Methods ​

MethodReturnDescription
isToast()boolCheck if this is a toast configuration
hasInput()boolCheck if this has an input configuration
hasPreConfirmRoute()boolCheck if this has a pre-confirm route
getPreConfirmRoute()?stringGet the pre-confirm route URL

Immutable Transformation ​

withoutPreConfirmRoute(): static ​

Return a new AlertConfig instance with the preConfirmRoute key removed. This is used by the Blade view to strip the route from the JavaScript output (since it's handled separately as a fetch() call):

php
$jsConfig = $alertConfig->withoutPreConfirmRoute();

JSON Structure ​

The toJson() output includes a wrapper:

json
{
    "config": {
        "title": "Hello",
        "icon": "success",
        "text": "World"
    },
    "type": "config"
}

The toSwalConfigJson() output is the raw config only:

json
{
    "title": "Hello",
    "icon": "success",
    "text": "World"
}

Made with ❤️ from Pakistan Released under the MIT License.