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

catto's avatar
Level 1

How to store data from webhook to database

Hi, currently I'm using 3rd party API for my laravel application. It will send a webhook to the url I provide for them. And the webhook is in json format.

My question is how to retrieve it in function and store it to database? because there's no documentation about how to do that.

And after searching in google I created a function to do it, and the code is like this (not working):

public function webhook_resi()
    {
        $response = file_get_contents('php://input');
        $data = json_decode($response->getBody());

        if($data->status == 'ON_PROCESS') {
            $status = 'on_delivery';
        } elseif($data->status == 'DELIVERED') {
            $status = 'delivered';
        }
        
        $order = Order::where('code', $response->external_id)->update([
            'delivery_status' => $status
        ]);

        $order_detail = OrderDetail::where('order_id', $order->id)->update([
            'delivery_status' => $status
        ]);

    }

And the webhook sended to my url is like this:

{
  "result": {
    "status": "ON PROCESS",
    "summary": {
      "awb": "150450014862419",
      "courier": "jne",
      "service": "REG19",
      "date": "2019-06-18 16:49:00",
      "desc": "SEPATU ",
      "amount": "43000",
      "weight": "1"
    },
    "detail": {
      "origin": "BATAM",
      "destination": "KUNINGAN, KAB KUNINGAN",
      "shipper": "03ALINA",
      "receiver": "DIAN RATNA KHRISTIAN "
    },
    "history": [
      {
        "date": "2019-06-18 16:49:00",
        "desc": "Shipment Received By Jne Counter Officer At [batam]",
        "location": "BATAM"
      },
      {
        "date": "2019-06-18 21:02:00",
        "desc": "Shipment Picked Up By Jne Courier [batam]",
        "location": "BATAM"
      },
      {
        "date": "2019-06-18 21:42:00",
        "desc": "Received At Sorting Center [batam]",
        "location": "BATAM"
      },
      {
        "date": "2019-06-19 00:40:00",
        "desc": "Processed At Sorting Center [batam]",
        "location": "BATAM"
      }
    ]
  },
  "external_id": "o-123-2019"
}

This is my first time using webhook, so I don't have any idea how to do it. Sorry and thank you

0 likes
3 replies
Nakov's avatar

I assume that's a POST route, so make sure you allow to be posted to that route from a 3d party by adding the endpoint in your VerifyCsrfToken middleware:

So if the route is this:

Route::post('/webhook', [YourController::class, 'webhook_resi']);

in VerifyCsrfToken middleware there is you should have this:

protected $except = [
        '/webhook',
];

Then in your method:

public function webhook_resi(Request $request) {
    $request->dd(); // this will dump the request data

    // or use this
    $data = $request->all();

}
1 like

Please or to participate in this conversation.