0

I am trying to get the return ($orders_box) of a function in my controller into my blade file javascript so that I can display it on a table on my blade file.

Right now the blade code looks like this:

    function checkOrderId () {
        const orderID = document.getElementById('PolarisTextField2').value;
        const response = localStorage.getItem('orders_box');
        fetch('/checkBalanceRoute/' + orderID)
            .then(response => console.log(response))
    }

and the controller code looks like:

{
    public function checkBalance ($orderID){
        $shop = Auth::user();
        $req = $shop->api()->rest('GET', "/admin/api/2022-04/orders/$orderID.json");
        $orders = $req['body']['orders'];
        $orders_box = [];
        foreach ($orders as $order) {
            $data = array(
                'Order Id' => $order->id,
                'Total Price' => $order->total_price,
                'Name' => $order->billing_address->name,
            );

        array_push($orders_box, $data);
        }
    return response() ->json([
        'success' => true,
        'order_box' => $orders_box
    ], 200);

    }

Any thoughts?

I appreciate the help.

1 Answers1

0

Instead of print_r($orders_box); use

return response()->json([
  'success'   => true,
  'order_box' => $order_box 
], 200);

This will return a response to your API.

Anshul Kumar
  • 109
  • 4
  • Ok, I did that but I am still not getting the response I would expect from the console.log. I get a response but it doesn't seem to have the $orders_box info. Is that code correct? – Henry Johnson May 07 '22 at 17:03