godforhire/toast

简单的 toast 通知

1.02 2022-07-06 07:44 UTC

This package is auto-updated.

Last update: 2024-09-07 14:37:11 UTC


README

本 composer 包为 Laravel 应用提供 Bootstrap 5 toast 通知和模态框。

此包基于 laracasts/flash 开发

安装

首先通过 Composer 引入此包。

composer require godforhire/toast

用法

在你的控制器中,在执行重定向之前,调用 toast() 函数。

public function store()
{
    toast('Toast message!');

    return home();
}

Toast 主题遵循 Bootstrap 的上下文类

  • toast('Toast 消息')->info():将 toast 主题设置为 "info"
  • toast('Toast 消息')->success():将 toast 主题设置为 "success"
  • toast('Toast 消息')->warning():将 toast 主题设置为 "warning"
  • toast('Toast 消息')->danger():将 toast 主题设置为 "danger"

如果没有传递类,则默认设置为 "info"。

除了叠加,toast 消息将在 5 秒后自动消失,但如果你想要改变这个设置,可以使用延迟方法

  • toast('消息')->danger()->delay(10000)

使用 important() 手动消失 toast 消息

  • toast('消息')->danger()->important()

要将消息渲染为叠加/模态框,使用

  • toast('Toast 消息')->overlay()

你还可以添加标题

  • toast()->overlay('模态消息', '模态标题')

有了这个消息已烤到会话中,你现在可以在你的视图(们)中显示它。因为 toast 消息和叠加非常常见,我们提供了一些模板来帮助你入门。你可以自由使用 - 并根据需要对其进行修改 - 这个模板。

@include('toast::message')

在你的应用程序模板中,添加以下代码来切换 toast 消息/模态框

    $('.toast-overlay-modal').modal('show');
    $('.toast').toast('show');

如果你需要修改 toast 消息的片段,你可以运行

php artisan vendor:publish --provider="godforhire\Toast\ToastServiceProvider"

请参考官方 Bootstrap 5 文档来定位 toast 消息:https://bootstrap.ac.cn/docs/5.1/components/toasts/

这两个包视图现在位于 resources/views/vendor/toast/ 目录。

多个 Toast 消息

需要将多个 toast 消息发送到会话?没问题。

toast('Toast message 1')->info();
toast('Toast message 2')->warning()->important();
toast('Toast message 3')->warning()->important();

return redirect('somewhere');

现在重定向时将看到三个 toast 消息。

你还可以混合叠加来显示两者

toast('Toast message 1')->info();
toast()->overlay('Toast message', 'Notice')->important();

return redirect('somewhere');

如果你重定向到另一个控制器并在那里创建 toast 消息,它将被添加到集合中。

多个 Modal 消息

虽然可以显示多个模态框,但它们将堆叠在一起。这也会导致多个背景叠加,使每个模态框的背景变得更暗。