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

mallorca's avatar

Listen to database change and refresh page

I am using laravel 5.1 and still pretty new to the framework.

When someone adds or updates a new article, I want the page where the article is displayed to be refreshed. Is it possible to do this using some kind of listener or "asking" x amounts of seconds if a update has been made? What would be a good practice to accomplish this? I would appreciate if someone could provide some code examples as well. Thanks!

I suppose this task can be achieved using web sockets but it's a bit overkill for my case since I just want a simple function for this one case.

0 likes
10 replies
bobbybouwmann's avatar

You can use jQuery or javascript and just ask a url every time if something has changes, if it has changed you get the new data.

mallorca's avatar

@bobbybouwmann Thanks for your reply. Do you have any examples of this approach and what kind of changes would it look for?

bobbybouwmann's avatar
Level 88

You would probably do something like this

function updatePost() {
    setInterval(function() {
        $.ajax({
            type: "GET",
            url: "posts/1/2015-11-24-00-00-00", // You add the id of the post and the update datetime to the url as well
            success: function(response) {
                // If not false, update the post
                if (response) {
                    // Update the h2 with the new title from the post
                            $("#article > h2").html(response.title);

                    // Update the div with the new body from the post
                            $("#article > .body").html(response.body);
                }
            }
        });
    }, 5000); // Do this every 5 seconds
}

Then in Laravel you do something like this

// Routes.php

Route::get('posts/{id}/{datetime}', 'PostsController@updated');

// PostsController.php
public function updated($id, $datetime)
{
    $post = Post::findOrFail($id);
    
    if ($post->updated_at > Carbon::createFromFormat('Y-m-d-H-i-s', $datetime)) {
        return $post;
    }

    return false;
}
2 likes
mallorca's avatar

@bobbybouwmann Thanks a lot for your example and your time! However I don't think that's going to work for me.

I have a list of articles that get loaded in a list for logged in users. All I want is that the page refreshes once a new article is added or removed.

Alternatively if it can be added / removed in the list somehow using ajax. Could you guide me in the right direction? Thanks!

bobbybouwmann's avatar

Instead of checking the updated_at you check the count of the articles. Or check the updated_at field of the last on the page with the last of the collection of articles. I'm sure you can think of a way to check this ;)

1 like
mallorca's avatar

@bobbybouwmann Thanks for your time to help! I'm really unsure how to approach this. Here is what my controller looks like:

public function articles()
{  
      $articles = Auth::user()->Articles;
        return view('pages.articles', compact('articles'));
}

Using your way, would I add a if statement to somehow check if more articles have been added to that user? What would the ajax request look like?

bobbybouwmann's avatar

You would create another route with a new method. This route is returning a view. You don't want that in your ajax response. You only want the data in your response

1 like
martinbean's avatar

@bobeta You’d be better off implementing a web socket-based solution that only updates a page if the channel it’s listening on receives a message (when you update an article, send a message).

You don’t want to be polling every x seconds because if 1,000 people are on your website, suddenly you’re server’s receiving 1,000 more HTTP requests every x seconds regardless of whether an article’s been published or not.

2 likes
mallorca's avatar

Thanks for all your replies, I really appreciate it!

Please or to participate in this conversation.