Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mallorca's avatar

Event listen when a user updates db query

hey great folks!

I have implemented pusher to try it out and I'm kind of new to laravel so please bare with me.

My controller:

    $lists = Auth::user()->lists;
        $users = Auth::user();
        return view('pages.lists’, compact(’lists’, 'users'));

And in my view I have:

@foreach ($lists as $list)              
<li><a href="{{ $list->address }}"><p>{{ $list->body }}</p></a></li>
@endforeach

The examples I've seen for pusher are using event listeners when someone hits a url and this is what I have using Laravel, Pusher and Vue by following Jeffs video: https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/12

What I would like to accomplish is listen to an event when a authorized user adds a new list in the db. The list itself gets saved in a mysql database right now using address and body columns. So when a user ads a new list item in the backend, he or she will will see the new list item in their frontend and a message.

How can I achieve this and in particular so that only the user gets a update on their own items and not someone elses? Also, would I need to refactor the blade code to work with vue?

0 likes
11 replies
mallorca's avatar

@bluepenlabs Thanks for your reply, I will look into that and consider it for the future, but for now I would like to try out Pusher since I already have it up and running.

Do you know how I could achieve this? Or maybe to simplify it: When the user adds a new contact in the backend that their frontend page just gets refreshed. The key part is that I don't understand how to listen to db events for a specific authorized user and to return actions to the same user..

nicsouthern's avatar

I had the same question myself. I haven't implemented this yet, but what I plan on doing is having a certain "channel" for each user (probably stored in the user table). Then, I'll have the javascript client listen on that channel (using socket, I imagine pusher would work the same way), and the backend code would broadcast on that user's specific channel. I plan on having it a 16 character random alphanumeric string, so users can't guess other user's channels.

Thats the communications part done, next you'd need to have your javascript client handle the new messages (see if it's a command to refresh the page, or push the new data to an array, etc).

That would be my solution, but someone else may have an easier/better solution.

mallorca's avatar

@nicsouthern Thanks for your idea.. Do you have any idea how I could accomplish this: define a event handler that checks if a list has been updated and then refresh a page of the user that the list belongs to? I have already specified that it's a one-to-many relationship in the model. Thanks!

mallorca's avatar

I would really appreciate it if I could get some help here

mallorca's avatar

@veve286 Thanks for the example and the time! I have already have pusher running, my question is more oriented to how to code the event listener for that task and how to display the update to only the users that the lists belong to

michaeldyrynda's avatar

Might be easiest to store (or use an accessor) in your user model. You can then check if you have an authenticated user in your layout view and build the user-specific channel listener. Likewise with firing the events - fire t using the authenticated user's channel identifier if one exists.

1 like
mallorca's avatar

@deringer Thanks, that sounds like a decent solution. Could you please provide some code examples so that I can get a better understanding of how it would work?

michaeldyrynda's avatar

This is pretty rough, and untested.

// In your model

public function getChannelAttribute()
{
    return sprintf('notifications-%d', $this->attributes['id']);
}


// In your view

@if(auth()->check())
    // Setup your pusher subscriber and get it to subscribe to auth()->user()->channel
@endif

Then when firing your event, make sure that it gets fired on $user->channel if there's an authenticated user.

masterpowers's avatar

First You Need to an a Hidden input for the Auth user ID in your Blade

Second In Your Controller method before you Return it Fire an Event

    event(new App\Events\UserHasAddedAList());

In Your Broadcasting Event File Add something like

 public function broadcastOn()
    {
        // return ['user-'. $this->user->id];
    }

This im not sure coz im using socket.io You Just need to make it pusher

 socket.on('rfn-chanel:user.id', function(data) {
          this.users.unshift(data);
      }.bind(this));

Please or to participate in this conversation.