Guys, I welcome everyone. In general, I faced such a question.

Simplify non-essential nuances. There is a statistic_visit table in the database. Approximately with this structure: id|visit|date That is, roughly speaking, each time you refresh the page, visit should be incremented where the date is today.

Probing the Internet and documentation, I thought that it would be more correct to do it through the Provider :

 class StatisticVisitProvider extends ServiceProvider { public function boot(Request $request) { $toDay = date("Ymd"); StatisticVisit::where('day', $toDay)->increment('view', 1); } public function register() { } } 

Everything works out, but there is one problem. In the database, with each update, it does not add to 1, but to a larger number, for example, 3, 80 . Apparently depending on what page I open.

And, accordingly, the conclusion suggests itself that the provider works more than once. That is, with each ajax request, or the loading of a widget, it executes an increment.

I ask you to suggest how to implement this correctly, if I am on the right path, if not then send.

Thank you very much in advance.

  • one
    I did the same in the mediator. I created the required class, added it to the $routeMiddleware and after the routes I needed, I assigned the created mediator. - Arendach
  • maybe it increases by the number of rows found by your query? - n.osennij

1 answer 1

This option has already been voiced in the comments above. I would also do it through middleware. Regarding the fact that you need to distinguish between ajax and the usual client synchronous http request, there are several options:

  • use / api root for ajax calls, and monitor only all remaining root. (respectively, apply middleware to them)
  • check the type of request in middlrware

https://stackoverflow.com/questions/29231587/laravel-check-if-ajax-request