rakeshthapac/ laratime
一款酷炫的Laravel包,用于通过WebSockets广播数据库更新,以实现实时系统
v1.0.0
2020-08-12 11:25 UTC
Requires (Dev)
- orchestra/testbench: ^5.3
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-13 07:08:59 UTC
README
通过使用WebSockets发送实时数据库更新,为您提供一个使系统实时化的酷炫方法
安装
- 在您的应用中设置WebSocket(Pusher,Laravel-Websockets)
- 在项目中要求此包
$ composer require rakeshthapac/laratime
- 现在安装小的npm包 laratime-js
$ npm install laratime-js
用法
- 只需将一个特质插入到您的模型中,您就可以准备出发了
use \rakeshthapac\LaraTime\Traits\LaraTimeable; // now all your database updates will be broadcasted through websockets
用法(前端 laratime-js)
- 导入laratime from laratime-js,并传入Echo实例
// in bootstrap.js // first create a instance of a echo with correct credentials import LaraTime from "laratime-js"; window.laratime = new LaraTime(window.Echo);
- 现在您可以监听数据库更改了
window.laratime .db("users") .on("added", (user) => addUser(user)) .on("updated", (user) => updateUser(user)) .on("deleted", (user) => deletesUser(user));
- 使用laratime-js,您可以使用 db(tableName) 监听一个表更新,并通过链式调用 on(eventName),您可以监听三个不同的事件
- 添加
- 更新
- 删除
- 您可以挂载一个回调函数,该函数接受添加(或更新或删除)的模型作为第一个参数,作为JavaScript对象。