jquery - 如何在 Laravel 中编辑传递给事件的变量?
问题描述
我想通过从数据库中获取路径并将原始图像解码为base64来获取图像的内容,以免将图像的真实路径暴露给最终用户。
$user->secret_image = getImage($user->secret_image); // This gets the base64 image of the path passed to it
event(new myEvent($user));
然后我有一个事件向她/他自己广播更新的数据。但问题是它不会将编辑后的数据传递到 jQuery 中的前端。当我file_put_contents('myevent.txt', json_encode($user));
进入时myEvent
,我看到该文件中的值是正确的,并且图像是 base64,但我不知道为什么我无法在 jQuery 中获取它。请注意,其他数据已正确传递,我可以在 jQuery 中获取它们,但只有未在此处编辑的参数。
我的事件:
class myEvent implements ShouldBroadcast
{
public $user;
public function __construct($user)
{
$this->user = $user;
}
}
jQuery:
window.Echo.channel('my-channel')
.listen('myEvent', (update) => {
let user = update.user;
$('[name="first_name"]').val(user.first_name);
console.log(user.secret_image);
});
解决方案
令人惊讶的是,我刚刚$user
从对象更改为数组并且它起作用了:|
推荐阅读
- rpgle - 如何在 ascii 中进行 base64 编码
- python - 如何在 Python 的不同文件函数中使用类中的变量?
- ruby - 在我的 MacOS 10.15.7 上升级 ruby 2.6 时遇到问题
- android-studio - jCenter & Bintray 正在关闭。怎么办?如果使用了第三方库,如何解决?
- cobol - 更新:如何修复 Cobol 中的排序和发布?
- django - Django 翻译:get_language
- java - 如何将单个 Java 文件转换为 MVC 设计?
- javascript - 禁用“关闭”按钮,直到模态视图 PDF 向下滚动
- amazon-web-services - terraform 不断强制使用默认参数对容器定义进行新资源/强制替换
- flutter - 如何在 Dart 中正确声明类的 List 类型属性?