1

I'm currently working on a code that displays users activity log. Ajax will call the route and gets the response (json) results. I need to be able to display the log records on admin dashboard without refreshing the page.

Ajax

 $(document).ready(function() {
        $.ajax({
            type: 'get',
            url:"{{ route('users.activity') }}",
            dataType: 'json',
            success: function (data) {
                $.each(data, function() {
                    $.each(this, function(index, value) {
                        console.log(value);
                        $('#activity').append('' +
                            '<div class="sl-item">' +
                            '<div class="sl-left bg-success"> <i class="ti-user"></i></div>' +
                            '<div class="sl-right">' +
                            '<div class="font-medium">' + value.causer.username + '<span class="sl-date pull-right"> ' + value.created_at + ' </span></div>' +
                            '<div class="desc">' + value.description + '</div>' +
                            '</div>' +
                            '</div>');
                    });
                });
            },error:function(){
                console.log(data);
            }
        });
 });
5
  • why you want to reload data? Has user new activities? filling a form? doing something? and it can be the event you are looking for instead of click. Commented Dec 22, 2018 at 10:01
  • 1
    You can use laravel pusher. It would better for your application. laravel.com/docs/5.7/broadcasting#driver-prerequisites Commented Dec 22, 2018 at 10:20
  • is data came from tabel or log file? Commented Dec 22, 2018 at 11:15
  • it's coming from a table Commented Dec 22, 2018 at 11:19
  • check my answer Commented Dec 22, 2018 at 11:23

2 Answers 2

2

If I understood, you can do this:

$(document).ready(function() {
    var requesting = false;
    var request = function() { 
    requesting = true;
    $.ajax({
        type: 'get',
        url:"{{ route('users.activity') }}",
        dataType: 'json',
        success: function (data) {
            $.each(data, function() {
                $.each(this, function(index, value) {
                    console.log(value);
                    $('#activity').append('' +
                        '<div class="sl-item">' +
                        '<div class="sl-left bg-success"> <i class="ti-user"></i></div>' +
                        '<div class="sl-right">' +
                        '<div class="font-medium">' + value.causer.username + '<span class="sl-date pull-right"> ' + value.created_at + ' </span></div>' +
                        '<div class="desc">' + value.description + '</div>' +
                        '</div>' +
                        '</div>');
                        requesting = false;
                });
            });
        },error:function(){
            console.log(data);
        }
    });
   };
  if(!requesting){
      setTimeout(request, 1000);
  }
 });

Every seconds it does a request to your users.activity route, so there is an update every second.

Sign up to request clarification or add additional context in comments.

5 Comments

you should use setTimeout instead of setInterval and make sure a request is NOT already in progress before firign a new request. So you reset the setTimeout when request finishes. This would be better
I tried something like this before, but the issue it keeps append the same records again an again.
Another approach could be a websocket like socket.io, when the user do an activity send a message to user that can see user's activities. (docs socket.io/docs). Maybe the problem is in the queries?
the query works fine with no issues, the issue is in my ajax code only
thank you for the effort, but i still need to refresh the page
1

You need to send last record id in server. so you can get only new data. my updated answer based on @stackedo answer .

$(document).ready(function () {
    var lastId = 0; //Set id to 0 so you will get all records on page load.
    var request = function () {
    $.ajax({
        type: 'get',
        url: "{{ route('users.activity') }}",
        data: { id: lastId }, //Add request data
        dataType: 'json',
        success: function (data) {
            $.each(data, function () {
                $.each(this, function (index, value) {
                    console.log(value);
                    lastId = value.id; //Change lastId when we get responce from ajax
                    $('#activity').append('' +
                        '<div class="sl-item">' +
                        '<div class="sl-left bg-success"> <i class="ti-user"></i></div>' +
                        '<div class="sl-right">' +
                        '<div class="font-medium">' + value.causer.username + '<span class="sl-date pull-right"> ' + value.created_at + ' </span></div>' +
                        '<div class="desc">' + value.description + '</div>' +
                        '</div>' +
                        '</div>');

                });
            });
        }, error: function () {
            console.log(data);
        }
    });
    };
   setInterval(request, 1000);
});

In controller add were condition to query :

UserActivity::where('id','>',$request->id)->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.