lara-pack/livewire-sweetalert 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lara-pack/livewire-sweetalert

最新稳定版本:v1.0.4

Composer 安装命令:

composer require lara-pack/livewire-sweetalert

包简介

Livewire Sweetalert

README 文档

README

Latest Version on Packagist Total Downloads

This Laravel package provides an easy way to trigger SweetAlert2 notifications from Livewire v3 or v4 components.

Requirements

  • PHP ^8.1
  • Laravel ^10.0 or ^11.0
  • Livewire ^3.0 or ^4.0
  • SweetAlert2 (must be installed in your frontend assets)

Installation

You can install the package via composer:

composer require lara-pack/livewire-sweetalert

The package will automatically register its Service Provider.

Frontend Preparation

Ensure you have included the SweetAlert2 library in your main layout (usually app.blade.php).

You can use CDN:

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

Or install it via NPM:

npm install sweetalert2

Usage

You can use the LaraPack\LivewireSweetalert\Alert class inside your Livewire components.

Success / Fail Notifications

Use the success or fail methods to display simple informational messages.

use LaraPack\LivewireSweetalert\Alert;
use Livewire\Component;

class CreateUser extends Component
{
    public function save()
    {
        // Save logic...

        Alert::success($this, 'Success!', 'User data has been saved.');
    }

    public function handleError()
    {
        Alert::fail($this, 'Failed!', 'An error occurred while processing data.');
    }
}

Confirmation Tool

Use the confirmation method to display a confirmation dialog that triggers other Livewire actions based on the user's choice.

use LaraPack\LivewireSweetalert\Alert;
use Livewire\Component;

class UserTable extends Component
{
    public function deleteConfirm($id)
    {
        Alert::confirmation(
            $this,
            Alert::ICON_WARNING,
            'Are you sure?',
            'Deleted data cannot be recovered!',
            'delete', // Event name if confirmed (Yes)
            'cancelDelete', // Event name if cancelled (No)
            'Yes, delete it!', // Confirmation button text (Optional)
            'Cancel' // Cancel button text (Optional)
        );
    }

    #[On('delete')]
    public function delete()
    {
        // Deletion logic...
        Alert::success($this, 'Deleted!', 'Data has been successfully deleted.');
    }

    #[On('cancelDelete')]
    public function cancelDelete()
    {
        // Optional: Logic if cancelled
    }
}

How It Works

This package uses middleware to inject a JavaScript script that listens for Livewire events (SwalInfo and SwalConfirm). When you call a method in the Alert class, this package dispatches an event to the frontend which is then caught by the script to display SweetAlert.

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固