afrizalmy/laratoast-jquery

laravel 的 toast jquery

v1.0.4 2021-12-30 17:13 UTC

This package is auto-updated.

Last update: 2024-09-29 05:43:13 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

laratoast

这是一个使用 https://kamranahmed.info/toast 库构建的 Laravel 通知包装器。

安装

使用 Composer

composer require afrizalmy/laratoast-jquery

然后,将服务提供者添加到 config/app.php 文件中。在 Laravel 5.5 及以上版本中,如果启用了包自动发现,此步骤可以跳过。

'providers' => [
    ...
    Afrizalmy\Laratoast\LaratoastServiceProvider::class
    ...
];

可选:如果您想修改默认配置,可以发布配置文件

php artisan vendor:publish --provider='Afrizalmy\Laratoast\LaratoastServiceProvider'

使用方法

在视图模板中包含 jQuery 和您的通知插件资源

  1. 添加您的样式链接标签或 @laratoast_css
  2. 添加您的脚本链接标签或 @laratoast_js
  3. 添加 @laratoast_render 以渲染您的通知
  4. 如果没有 jQuery,则添加 @laratoast_jquery
  5. 在控制器内部使用 laratoast() 辅助函数来设置 info、success、warning 或 error 的 toast 通知
// Display an info toast
laratoast()->info("Are you married?","Information","bottom-right");

例如

<?php

namespace App\Http\Controllers;

use App\Post;
use App\Http\Requests\PostRequest;
use Illuminate\Database\Eloquent\Model;

class PostController extends Controller
{
    public function store(PostRequest $request)
    {
        $post = Post::create($request->only(['title', 'body']));

        if ($post instanceof Model) {
            laratoast()->success("Data has been saved successfully!","Success","bottom-right",['textAlign'=>'left']);

            return redirect()->route('posts.index');
        }
        laratoast()->error("An error has occurred please try again later.","Error!","bottom-center");

        return back();
    }
}

之后,将 @laratoast_render 添加到视图的底部以实际渲染 laratoast 通知。

<!doctype html>
<html>
    <head>
        <title>afrizalmy/laratoast-jquery</title>
        @laratoast_jquery <!-- if you don't have jquery -->
        @laratoast_css
    </head>
    <body>
        
    </body>
    @laratoast_js
    @laratoast_render
</html>

选项

// laratoast()->success("Message","Title","position", "options"); 
laratoast()->success("Success notification test","Success","bottom-left"); 
// laratoast()->error("Message","Title","position", "options"); 
laratoast()->error("Error notification test","Error","bottom-right",['textAlign'=>'center']);
// laratoast()->warning("Message","Title","position", "options");       
laratoast()->warning("Warning notification test","Warning","bottom-center",['textAlign'=>'right']); 
// laratoast()->info("Message","Title","position", "options"); 
laratoast()->info("Info notification test","Info","top-left",['textAlign'=>'left']);

您可以在 config/laratoast 文件中查看选项

捐赠

https://saweria.co/afrizalmy