Am coding a forum using laravel, please how do I go about a function that count the thread visits?

mista_ceemista_cee Posts: 53
edited May 2019 in Chats

Hey guys...  Am coding a forum using laravel,  please how do I go about a function that count the number of people who visit a thread...? 

Comments

  • dhtmldhtml Posts: 1,019
    edited October 2018

    I can answer this question in many ways, but first you need to tell us the type of data you are looking for. But a likely solution is this:


    step 1: create a table called hits with fields - ID (autoincrement), thread_id (int), user_id (int), time (int)

    Step 2: each time the thread is visited, you can insert an entry into the table id, thread_id, user_id, time 
    As for the time, you can use mysql timestamp or whatever you like


    Step 3: to get the total number of people who visit the thread, do something like:

    select count(*) from hits where thread_id=1 group by user_id (untested)

  • mista_ceemista_cee Posts: 53
    edited October 2018

    Thanks sir...  Buh how I can get the hits is the problem,  what php function do I use? 

  • sunnysunny Posts: 25
    edited October 2018
    From my little knowledge you can insert a code to update a thread hit column on every view. But you probably have to check it's not the same device or in other cases user. .
  • mrcodesmrcodes Posts: 99
    edited October 2018
    dhtml wrote:

    I can answer this question in many ways, but first you need to tell us the type of data you are looking for. But a likely solution is this:


    step 1: create a table called hits with fields - ID (autoincrement), thread_id (int), user_id (int), time (int)

    Step 2: each time the thread is visited, you can insert an entry into the table id, thread_id, user_id, time 
    As for the time, you can use mysql timestamp or whatever you like


    Step 3: to get the total number of people who visit the thread, do something like:

    select count(*) from hits where thread_id=1 group by user_id (untested)


    I would also suggest this too

  • mrcodesmrcodes Posts: 99
    edited October 2018
    mista_cee wrote:

    Thanks sir...  Buh how I can get the hits is the problem,  what php function do I use? 

    From a laravel point of view, you can do this below


    $hits = DB::select('select COUNT(*) AS total_hits from hits WHERE thread_id=2')->get();

    OR

    $hits = DB::table('hits')->where('thread_id', 2)->get();

    and get your results with 

    $hits->total_hits;
  • dhtmldhtml Posts: 1,019
    edited October 2018
Sign In or Register to comment.