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

Sun's avatar
Level 1

How to auto reload a page when the data is changed?

I am using a webhook to receive incoming data and update my existing table from my controller. I need to figure out how to automatically refresh the page when the webhook does it. Currently, I have to manually refresh the page to see the new coming data. I checked on many JS, but they are not working. Any ideas? Thank you, guys.

0 likes
5 replies
Cronix's avatar

It really depends. We don't know anything about your webhook or how it's called or what you're doing with it, so showing your code would be helpful if you want something to work with your specific scenario.

Sun's avatar
Level 1

My table is listed below. I kinda want to create a session message when there is a data change, the page should reload with the session message.

Controller

public function receive()
    {
        $message = \Nexmo\Message\InboundMessage::createFromGlobals();

        $org_number = $message->getTo();
        $org_number = substr($org_number, 1);
        $org_number = "(" . $org_number;
        $org_number = substr_replace($org_number, ') ', 4, 0);
        $org_number = substr_replace($org_number, '-', 9, 0);
        $org_id = Organization::where('toll_free_number', '=', $org_number)->first()->id;

        $msg = $message->getBody();

        $user_number = $message->getFrom();
        $user_number = substr($user_number, 1);
        $user_number = "(" . $user_number;
        $user_number = substr_replace($user_number, ') ', 4, 0);
        $user_number = substr_replace($user_number, '-', 9, 0);

        $cancel_msg = MsgTemplate::where('org_id', $org_id)->where('purpose_id', 3)->first()->body;

        if (strcasecmp($msg, '2') == 0) {
            $waiting_lists = Waitinglist::where('org_id', $org_id)->where('phone_number', $user_number)->get();

            if ($waiting_lists->isNotEmpty()) {

                $reply = Nexmo::message()->send($message->createReply($cancel_msg));

                foreach ($waiting_lists as $waiting_list) {
                    $waiting_list->status_id = 6;
                    $waiting_list->notified_at = Carbon::now();

                    $waiting_list->save();
                }
            }
        }
    }

web.php

Route::post('/waitinglist/receive', ['as' => 'waitinglist.receive', 'uses' => 'WaitingListController@receive']);

index.php

@foreach($wait_customers as $wait_customer)
                <tr>
                    <td style="vertical-align: middle">{{$wait_customer->name}}</td>
                    <td style="vertical-align: middle">{{$wait_customer->phone_number}}</td>
                    <td style="vertical-align: middle">{{$wait_customer->table_size}}</td>
                    <td style="vertical-align: middle">{{$wait_customer->started_at}}</td>
                    <td style="text-align: center;">
                        <button data-toggle="modal" data-target="#editModal" class="edit-modal btn btn-outline-primary" data-id="{{$wait_customer->id}}" data-phone_number="{{$wait_customer->phone_number}}" data-table_num="{{$wait_customer->table_num}}">
                            <i class="fa fa-paper-plane-o" aria-hidden="true"></i> Send Notification</button>
                        <a href="{{action('WaitingListController@cancel', $wait_customer)}}" class="btn btn-outline-warning"><i class="fa fa-ban" aria-hidden="true"></i> Cancel</a>
                    </td>
                </tr>
            @endforeach 
Cronix's avatar
Cronix
Best Answer
Level 67

I think the best way to really do this is with broadcasting: https://laravel.com/docs/5.5/broadcasting

Another option is to use ajax on a timer to check for changes and update the page if there are any. This isn't as good as broadcasting because in your webhook, you can basically broadcast and event that "I've been updated" and the listener in the view would trigger a reload or something when it receives the notice.

The thing is, your view is already drawn and sent to your browser. Storing something in session in the webhook won't do anything because the view is already created and won't know about the updated session variable. So the ajax and broadcasting solutions get around that and are the only way to update a view that's already in the browser.

Sun's avatar
Level 1

Interesting! I will definitely try broadcasting and let you know if I have questions. I will post my answer here after the tryout. Thank you.

Please or to participate in this conversation.