How can I prevent user data from being inserted into the database every time the page is reloaded?


Brad_Fresh

I have this function that checks if the user can join the channel and then inserts this data into the database. The problem is that it gets inserted every time I reload the page. How to prevent this from happening?

Broadcast::channel('chat', function ($user) {
     $ip = Request::ip();
     $time = now();
  if (auth()->check()) { 
    UserInfo::storeUser();
      return [
        'id' => $user->id,
        'ip' => $ip,
        'name' => $user->name,
        'joined' => $time
    ];
  }
});
Supernova

Basically, you need any schema to determine that you have stored information about that user. DB , session or even auth()->user() objects ( depending on the use case ) can store this data.

Take session as an example:

Broadcast::channel('chat', function ($user) {
    $ip = Request::ip();
    $time = now();
    if (auth()->check() && !session()->has('user_id')){ 
        UserInfo::storeUser();
        session()->put('user_id',$user->id);
        return [
           'id' => $user->id,
           'ip' => $ip,
           'name' => $user->name,
           'joined' => $time
       ];
    }
});

and on logout:

session()->forget('user_id')

Remember, this is a basic example without much context.

Related


How can I get a random beer every time the page is reloaded?

ChuChu I'm trying to get a random beer and a list of 20 beers from the Punk Beer API and display it on a page. But for some reason the API with random URL always returns the same beer (ID: 221, Name: Blitz series). I'm confused why it's not a different beer ev

How can I get a random beer every time the page is reloaded?

ChuChu I'm trying to get a random beer and a list of 20 beers from the Punk Beer API and display it on a page. But for some reason the API with random URL always returns the same beer (ID: 221, Name: Blitz series). I'm confused why it's not a different beer ev

How can I prevent the page from being regenerated in the loader?

DJ Yosemite I am developing a Qt app on Win/Android. My question is simple. When my app starts, first there is a login page to welcome you. ServerInfo.qmlOpen in Loader if you want to configure server settings . The login page and ServerInfo are loaded in the

How can I prevent the page from being regenerated in the loader?

DJ Yosemite I am developing a Qt app on Win/Android. My question is simple. When my app starts, first there is a login page to welcome you. ServerInfo.qmlOpen in Loader if you want to configure server settings . The login page and ServerInfo are loaded in the

How can I prevent a directory from being deleted by the user?

pandia Suppose the directory was created dir1by sudoDesktop . sudo mkdir dir1 Then I apply chownand it chmodlooks like this: sudo chown root:root dir1 sudo chmod go-rwx dir1 Now dir1only accessible by owner root. $ ls -ld dir1 drwx------ 2 root root 4096 Jul

How can I prevent a directory from being deleted by the user?

pandia Suppose the directory was created dir1by sudoDesktop . sudo mkdir dir1 Then I apply chownand it chmodlooks like this: sudo chown root:root dir1 sudo chmod go-rwx dir1 Now dir1only accessible by owner root. $ ls -ld dir1 drwx------ 2 root root 4096 Jul

How to prevent invalid data from being inserted into SQL

Gamma In my project I need to check condition dynamically. To achieve this create a table as shown below. CREATE TABLE myconditions ( conditionid INT IDENTITY PRIMARY KEY CLUSTERED, minvalue INT, maxvalue INT, result INT )

How to prevent invalid data from being inserted into SQL

Gamma In my project I need to check condition dynamically. To achieve this create a table as shown below. CREATE TABLE myconditions ( conditionid INT IDENTITY PRIMARY KEY CLUSTERED, minvalue INT, maxvalue INT, result INT )

How can I prevent data from being written to the directory?

Andreas Hartman Or: How can I make the directory write-protected for all users until unlocked again? Mano You can simply use (1) chmod a-w directory This will make the directory unwritable to everyone. Grant write permissions to all users: chmod a+w director

How can I prevent data from being written to the directory?

Andreas Hartman Or: How can I make the directory write-protected for all users until unlocked again? Mano You can simply use (1) chmod a-w directory This will make the directory unwritable to everyone. Grant write permissions to all users: chmod a+w director