Update CMS Item Fields Data via API with Laravel Framework and Webflow PHP SDK

Hello everyone,
I am trying to do a bulk change in one collection via API. This is the piece of code:

public function UpdateWebflow(Request $request){
            $webflow                = 'myAPIKey';
            $siteid                 = 'mySiteId';
            $collectionId           = 'myCollectionId';
            $getItemInCollection    = $webflow->itemsAll($collectionId);

            $categories = Category::latest()->get();
            $count = 0;
            $dataExist = [];

            foreach($getItemInCollection as $key => $item){
                $dataExist[] = ['_id' => $item->_id];
            }

            foreach($categories as $cat){
                $match = $this->search($dataExist, '_id', $cat->webflow_id);
                // var_dump($match);

                if(count($match) != 0){
                    $data = [
                        'name'          => $cat->name,
                        'ecode'         => $cat->code,
                        'eid-2'          => strval($cat->id),
                    ];
                    $count++;
                    $UpdateCollectionItem = $webflow->patchItem($collectionId, $itemId, $data);
                    // var_dump($UpdateCollectionItem);
                }
            }

            if ($count == count($categories)) {
                return response()->json([
                    'success'   => true,
                    'message'   => 'Data Updated!',
                    'data'      => count($categories).' Categories Data'
                ], 201);
            }else if ($count < count($categories)){
                return response()->json([
                    'success'   => true,
                    'message'   => 'Data Updated!',
                    'data'      => $count.' Categories Data'
                ], 201);
            }else {
                return response()->json([
                    'success' => false,
                    'message' => 'Failed!',
                ], 400);
            }
        }

but the code above only update last item (Please see the response when i do var_dump() below):

object(stdClass)#333 (17) {
  ["_archived"]=>
  bool(false)
  ["_draft"]=>
  bool(false)
  ["name"]=>
  string(19) "Uncategorise-update"
  ["slug"]=>
  string(12) "uncategorise"
  ["updated-on"]=>
  string(24) "2020-11-24T02:28:08.045Z"
  ["updated-by"]=>
  string(31) "censored"
  ["created-on"]=>
  string(24) "2020-10-30T07:50:07.036Z"
  ["created-by"]=>
  string(31) "censored"
  ["published-on"]=>
  string(24) "2020-11-13T08:53:54.747Z"
  ["published-by"]=>
  string(31) "censored"
  ["code-erp"]=>
  string(1) "X"
  ["id-erp"]=>
  string(1) "9"
  ["ecode"]=>
  string(1) "X"
  ["eid"]=>
  int(9)
  ["eid-2"]=>
  string(1) "9"
  ["_cid"]=>
  string(24) "censored"
  ["_id"]=>
  string(24) "censored"
}
object(stdClass)#346 (17) {
  ["_archived"]=>
  bool(false)
  ["_draft"]=>
  bool(false)
  ["name"]=>
  string(13) "Dan Lain-lain"
  ["slug"]=>
  string(12) "uncategorise"
  ["updated-on"]=>
  string(24) "2020-11-24T02:28:09.115Z"
  ["updated-by"]=>
  string(31) "censored"
  ["created-on"]=>
  string(24) "2020-10-30T07:50:07.036Z"
  ["created-by"]=>
  string(31) "censored"
  ["published-on"]=>
  string(24) "2020-11-13T08:53:54.747Z"
  ["published-by"]=>
  string(31) "censored"
  ["code-erp"]=>
  string(1) "X"
  ["id-erp"]=>
  string(1) "9"
  ["ecode"]=>
  string(1) "D"
  ["eid"]=>
  int(9)
  ["eid-2"]=>
  string(1) "7"
  ["_cid"]=>
  string(24) "censored"
  ["_id"]=>
  string(24) "censored"
}
object(stdClass)#333 (17) {
  ["_archived"]=>
  bool(false)
  ["_draft"]=>
  bool(false)
  ["name"]=>
  string(10) "ATK-update"
  ["slug"]=>
  string(12) "uncategorise"
  ["updated-on"]=>
  string(24) "2020-11-24T02:28:17.941Z"
  ["updated-by"]=>
  string(31) "censored"
  ["created-on"]=>
  string(24) "2020-10-30T07:50:07.036Z"
  ["created-by"]=>
  string(31) "censored"
  ["published-on"]=>
  string(24) "2020-11-13T08:53:54.747Z"
  ["published-by"]=>
  string(31) "censored"
  ["code-erp"]=>
  string(1) "X"
  ["id-erp"]=>
  string(1) "9"
  ["ecode"]=>
  string(1) "5"
  ["eid"]=>
  int(9)
  ["eid-2"]=>
  string(1) "6"
  ["_cid"]=>
  string(24) "censored"
  ["_id"]=>
  string(24) "censored"
}

i use webflow php sdk on github and laravel framework. Anyone please help to solve this issue.


My guess is that $categories = Category::latest()->get(); returns only one category.

Thank you Marc, i forgot to change that line. That was my mistake :smiley:

Glad I could help. :smile:

These kinds of errors happen all the time.

One trick I use, when patching code to test something that I’ll need to undo later, is leave a comment with a tag that you can search for later, example: // UNDO: lets try one category for now

Then, before deploying, run grep -r UNDO: . in your project directory to make sure all those patches have been removed.