Skip to content

AlertBuilder API

The AlertBuilder is the main fluent builder for constructing SweetAlert2 configurations. Every method returns $this for chaining, enabling natural method chaining that mirrors Laravel Eloquent's expressive API.

Import

php
use RealRashid\SweetAlert\Builders\AlertBuilder;

Constructor

php
new AlertBuilder(?AlertFlasher $flasher = null)

Creates a new AlertBuilder instance with default configuration from config/sweetalert.php. Typically accessed via the Alert facade rather than direct instantiation.

Factory Methods

MethodReturnDescription
make()staticCreate a new builder instance from the container
reset()staticReset the builder to default configuration

Core Configuration

MethodParametersDescription
title(string $title)The alert titleSet the heading text
text(string $text)Description textSet the body text below the title
icon(string|AlertType $type)Icon typeSet the alert icon
success(string $title = '', string $text = '')Optional title & textSet icon to success
error(string $title = '', string $text = '')Optional title & textSet icon to error
warning(string $title = '', string $text = '')Optional title & textSet icon to warning
info(string $title = '', string $text = '')Optional title & textSet icon to info
question(string $title = '', string $text = '')Optional title & textSet icon to question (auto-shows cancel)
html(string $html)HTML contentSet HTML body (replaces text)
toHtml()Convert text content to HTML
view(string $view, array $data = [], array $mergeData = [])Blade view name & dataRender a Blade view as HTML content

Convenience Factory Methods

MethodReturn TypeDescription
toast(string $title = '', ?string $icon = null)ToastBuilderCreate a toast notification builder
input(string $title = '', string|InputType $inputType = InputType::Text)InputBuilderCreate an input dialog builder
confirm(string $title = '', ?string $text = null)staticCreate a confirm dialog with cancel button
confirmDelete(string $title, ?string $text = null)staticCreate a delete confirmation (auto-flashes)
preConfirmRoute(string $route)staticSet server-side confirm validation route
preDenyRoute(string $route)staticSet server-side deny validation route
progressSteps(array $steps)staticSet progress step indicators
currentProgressStep(int $index)staticSet current step index

Flash & Serialize

MethodReturnDescription
flash(string $type = 'config')staticFlash configuration to the session
toArray()arrayGet filtered config array (removes empty values)
toJson()stringGet config as JSON string
getConfig()arrayGet raw config array (including empty values)

Backward Compatibility

MethodDescription
alert(string $title = '', string $text = '', ?string $icon = null)Legacy method — sets title/text/icon without flashing (call ->flash() explicitly)

Inherited from HasTimer

MethodParametersDescription
timer(int $milliseconds)Timer in msSet auto-close timer
autoClose(int $milliseconds = 5000)Timer in msAlias for timer()
timerProgressBar(bool $enabled = true)Enable/disableShow timer progress bar
persistent(bool $showConfirmBtn = true, bool $showCloseBtn = false)Button visibilityDisable timer, ESC, and outside click

Inherited from HasPosition

MethodParametersDescription
position(string|Position $position)Position valueSet alert position
top()Position: top center
topStart()Position: top left
topEnd()Position: top right (RTL-aware)
topLeft()Position: top left
topRight()Position: top right
center()Position: center
centerStart()Position: center left (RTL-aware)
centerEnd()Position: center right (RTL-aware)
centerLeft()Position: center left
centerRight()Position: center right
bottom()Position: bottom center
bottomStart()Position: bottom left (RTL-aware)
bottomEnd()Position: bottom right (RTL-aware)
bottomLeft()Position: bottom left
bottomRight()Position: bottom right

Inherited from HasButtons

MethodParametersDescription
showConfirmButton(string $text = 'OK', string $color = '#3085d6')Button text & colorShow confirm button
confirmButton(string $text = 'OK', string $color = '#3085d6')Button text & colorAlias for showConfirmButton()
showCancelButton(string $text = 'Cancel', string $color = '#aaa')Button text & colorShow cancel button
cancelButton(string $text = 'Cancel', string $color = '#aaa')Button text & colorAlias for showCancelButton()
showDenyButton(string $text = 'Deny', string $color = '#dd6b55')Button text & colorShow deny button
denyButton(string $text = 'Deny', string $color = '#dd6b55')Button text & colorAlias for showDenyButton()
showCloseButton(string $ariaLabel = 'Close this dialog')ARIA labelShow close button
hideCloseButton()Hide close button
closeButtonAriaLabel(string $label)ARIA labelSet close button aria-label independently
reverseButtons()Reverse confirm/cancel order
buttonsStyling(bool $enabled = true)Enable/disableToggle default button styling
focusConfirm(bool $focus = true)Focus stateFocus confirm button
focusCancel(bool $focus = true)Focus stateFocus cancel button
showLoaderOnConfirm(bool $enabled = true)Enable/disableShow loader on confirm button

Inherited from HasAnimation

MethodParametersDescription
animation(string $show, string $hide)Animate.css class namesSet show/hide animations
disableAnimation()Disable all animations
showClass(array $classes)Class mapSet custom show classes
hideClass(array $classes)Class mapSet custom hide classes

Inherited from HasStyling

MethodParametersDescription
width(string $width)CSS valueSet modal width
padding(string $padding)CSS valueSet modal padding
background(string $color)CSS colorSet background color
color(string $color)CSS colorSet text color
heightAuto(bool $enabled = true)Enable/disableSet height to auto
customClass(array $classes)Class mapSet custom CSS classes
iconHtml(string $html)HTML stringSet custom icon HTML
iconColor(string $color)CSS colorSet icon color
imageUrl(string $url, ?int $w = null, ?int $h = null, ?string $alt = null)Image configSet alert image
addImage(string $url, int $w = 400, int $h = 200, ?string $alt = null)Image configSet image with dimensions
footer(string $html)HTML stringSet footer content
grow(string $direction = 'false')DirectionSet grow direction
backdrop(mixed $backdrop = true)Bool or CSSSet backdrop config
allowEscapeKey(bool $allow = true)Enable/disableAllow ESC to close
allowOutsideClick(bool $allow = true)Enable/disableAllow outside click to close
stopPropagation(bool $enabled = true)Enable/disableStop keydown propagation

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