定制 gartservice/livewire-sweet-alerts 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

gartservice/livewire-sweet-alerts

Composer 安装命令:

composer require gartservice/livewire-sweet-alerts

包简介

A Laravel Livewire package that allows you to show SweetAlert2 notifications directly from Livewire components.

README 文档

README

A Laravel 11+ / Livewire 3 trait to easily trigger SweetAlert2 modals from PHP code.

Livewire Sweet Alerts Demo

📦 Installation

composer require gartservice/livewire-sweet-alerts

⚙️ Setup

1. Include SweetAlert2 in your Blade layout

If not using Vite, add this to your main layout (e.g. resources/views/layouts/app.blade.php):

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

2. Or install with Vite

npm install sweetalert2

Then in your resources/js/app.js:

import Swal from 'sweetalert2';
window.Swal = Swal;

Run:

npm run dev

🚀 Usage

Add Trait to Livewire Component

use Gartservice\LivewireSweetAlert\LivewireSweetAlert;

class ExampleComponent extends Component
{
    use LivewireSweetAlert;

    public function save()
    {
        $this->alert('Saved!', 'Your data was saved successfully.', 'success');
    }
}

Confirm Modal Example

$this->confirm(
    'Are you sure?',
    'This action cannot be undone.',
    'warning',
    'Yes, delete it!',
    'Cancel',
    'deleteConfirmed()', // Make sure this method is not public (should be protected or private)
    'cancelled()'
);

Alert with Callbacks (didClose)

$this->alert(
    'Notice',
    'Check your console.',
    'info',
    [
        'didClose' => '() => { console.log("Alert was closed") }' // Must be a valid JS function string, e.g., '() => { ... }'
    ]
);

Confirm Modal with Callbacks (didOpen, didClose)

$this->confirm(
    'Confirm Action',
    'Do you want to proceed?',
    'question',
    'Proceed',
    'Cancel',
    'proceedConfirmed', // Make sure this method is not public (should be protected or private)
    'cancelledAction',
    [
        'didOpen' => '() => { console.log("Opened!") }',
        'didClose' => '() => { console.log("Closed!") }'
    ]
);

🧪 Full Demo Example

Component Class

namespace App\Livewire;

use Livewire\Component;
use Gartservice\LivewireSweetAlert\LivewireSweetAlert;

class SweetAlertDemo extends Component
{
    use LivewireSweetAlert;

    public function showSimpleAlert()
    {
        // Any logic can be here
        $this->alert('Hello!', 'This is a basic alert.', 'success');
    }

    public function showAlertWithCallback()
    {
        $this->alert('Notice', 'Check your console after closing.', 'info', [
            'didClose' => '() => { console.log("Alert was closed!"); console.log("This is a callback function!"); }'
        ]);
    }

    public function showConfirmAlert()
    {
        // Show confirm message before processing
        $this->confirm(
            'Are you sure?',
            'This action is irreversible.',
            'warning',
            'Yes, go ahead!',
            'Cancel',
            'confirmedAction',
            'cancelledAction'
        );
    }
    // This method can be run only after confirm
    protected function confirmedAction()
    {
        // Perform any additional logic after confirmation, such as auditing user actions
        $this->alert('Confirmed', 'You agreed!', 'success');
    }

    public function cancelledAction()
    {
        // Show message if user canceled action
        $this->alert('Cancelled', 'You backed out.', 'error');
    }

    public function render()
    {
        return view('livewire.sweet-alert-demo');
    }
}

Blade View

<div class="p-6 space-y-4">
    <button type="button" wire:click="showSimpleAlert()" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded">Simple Alert</button>

    <button wire:click="showAlertWithCallback" class="bg-purple-500 hover:bg-purple-600 text-white px-4 py-2 rounded">Alert with Callback</button>

    <button wire:click="showConfirmAlert" class="bg-yellow-500 hover:bg-yellow-600 text-white px-4 py-2 rounded">Confirmation</button>

    <button wire:click="confirmedAction" class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded">Run Action Directly</button>
</div>

✅ Compatibility

  • Laravel 11+
  • PHP 8.3+
  • Livewire 3+

📄 License

MIT License. Created by Gartservice.

gartservice/livewire-sweet-alerts 适用场景与选型建议

gartservice/livewire-sweet-alerts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gartservice/livewire-sweet-alerts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gartservice/livewire-sweet-alerts 我们能提供哪些服务?
定制开发 / 二次开发

基于 gartservice/livewire-sweet-alerts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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