laravel - 使用观察者创建新记录是否合乎逻辑?
问题描述
我正在尝试做一个“通知”系统,我得到了一些条件,当这些条件被触发时,我会在“通知”表中创建一个通知记录。
例如; 当我的产品数量低于“关键库存水平”时,它将在“通知”表中创建新记录。
我将使用下面的代码:
Product.php 模型:
use App\Traits\RecordSignature;
class Product extends Model
{
use RecordSignature, TrimScalarValues;
....
记录签名.php
namespace App\Traits;
use app\Observers\ModelSignature;
trait RecordSignature
{
public static function bootObservantTrait()
{
static::observe(new ModelSignature);
}
}
模型签名.php
namespace App\Observers;
class ModelSignature {
protected $userID;
public function __construct($userID){
$this->userID = Auth::id();
}
public function created($model)
{
if($model->critical > $model->quantity){
$notif = new Notification();
$notif->message = 'your product is below the critical stock';
$notif->save();
}
}
}
这是实现它的好方法吗?(我将为更多的表格制作更多的观察者)或者这些条件检查是否有任何“更好的方法”?
解决方案
推荐阅读
- ssh - 将流量从办公室重定向到家
- reactjs - Magento PWA graphql 问题
- angular - 无法使用 jasmine 以角度测试服务方法
- python - 在python中下载数据时的索引问题
- python - ImportError:DLL 加载失败:%1 不是有效的 Win32 应用程序。psspy
- java - 为什么我的对象在我的 Swing UI 上重叠?
- reactjs - 118:51:预计在箭头函数 array-callback-return 的末尾返回一个值
- javascript - Nodemailer - 提交表单验证
- javascript - Gitlab 变量和 Vanilla Javascript
- python-3.x - 如何在pygame中以同一毫秒播放多个声音文件?