首页 > 解决方案 > 如何在 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);
    });

标签: jquerylaraveleventsbroadcast

解决方案


令人惊讶的是,我刚刚$user从对象更改为数组并且它起作用了:|


推荐阅读