kodfire
kodfire

Reputation: 1783

How to edit variable passed to event in Laravel?

I want to get the content of an image by getting the path from database and decode the original image to base64 in order not to expose the real path of image to the end-user.

$user->secret_image = getImage($user->secret_image); // This gets the base64 image of the path passed to it
event(new myEvent($user));

Then I have an event to broadcast updated data to her/himself. But the problem is that it doesn't pass the edited data to front-end in jQuery. When I file_put_contents('myevent.txt', json_encode($user)); in myEvent, I see that the value inside that file is correct and the image is base64 but I don't know why I can't get it in jQuery. Note that other data are passed correctly and I can get them in jQuery but only parameters not edited there.

myEvent:

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);
    });

Upvotes: 0

Views: 151

Answers (1)

kodfire
kodfire

Reputation: 1783

Surprisingly I just changed $user from object to an array and it worked :|

Upvotes: 0

Related Questions