1

I'm trying to generate a Resource with the same model. But in the response it adds a key which i don't know where it comes from.

Here is the api call:

Route::get("/definitions/{companyId}", function($companyId) {
    $definitions = CompanyDefinition::all()->where('company_id', $companyId);
    return new CompanyDefinitionResource($definitions);
});  

Here is the CompanyDefinitionsResource.php:

public function toArray($request)
    {
        
        $companies = $this;
        return [
            // 'name' => $this->name,
            'bankBrokers' => $companies->where('type', 1),
            'shippers' => $companies->where('type', 2),
            'accompanyingDocuments' => $companies->where('type', 3),
        ];
    }

and here is the response from insomnia:

{
    "bankBrokers": [
        {
            "id": "cc1abf9e-819b-495d-b632-bc010d3b703d",
            "company_id": "b054b9db-4e7c-4143-89ac-e82aefb8dab4",
            "name": "Yapı Kredi Bankası",
            "type": 1,
            "created_at": null,
            "updated_at": null
        },
        {
            "id": "b78ee82e-4d91-4f45-827f-202eb581b83b",
            "company_id": "b054b9db-4e7c-4143-89ac-e82aefb8dab4",
            "name": "Garanti Bankası",
            "type": 1,
            "created_at": null,
            "updated_at": null
        },
        {
            "id": "ad08e76d-f1ea-43a3-ac11-84685167086c",
            "company_id": "b054b9db-4e7c-4143-89ac-e82aefb8dab4",
            "name": "Akbank",
            "type": 1,
            "created_at": null,
            "updated_at": null
        }
    ],
    "shippers": {
        "3": {
            "id": "cfa9e413-54be-4284-a3ad-3b0c276c0e52",
            "company_id": "b054b9db-4e7c-4143-89ac-e82aefb8dab4",
            "name": "DHL",
            "type": 2,
            "created_at": null,
            "updated_at": null
        },
        "4": {
            "id": "87b4a5c0-f8cd-4481-809e-eee8f2baa72f",
            "company_id": "b054b9db-4e7c-4143-89ac-e82aefb8dab4",
            "name": "UPS",
            "type": 2,
            "created_at": null,
            "updated_at": null
        }
    },
    "accompanyingDocuments": {
        "5": {
            "id": "bc345648-1003-40e0-88ec-8c3db62931bc",
            "company_id": "b054b9db-4e7c-4143-89ac-e82aefb8dab4",
            "name": "Evrak 1",
            "type": 3,
            "created_at": null,
            "updated_at": null
        },
        "6": {
            "id": "fd2a895f-b62d-4db3-928a-1793c141fa5c",
            "company_id": "b054b9db-4e7c-4143-89ac-e82aefb8dab4",
            "name": "Evrak 2",
            "type": 3,
            "created_at": null,
            "updated_at": null
        }
    }
}

As you can see from the result there are "3" - "4" - "5" keys added to the response. Also in "bankBrokers" you can see it's an array but the others are object. Why it could be added and how can i resolve this issue?

Thanks.

1
  • 1
    It's because your array's or collection's index is not started from "0" You probably apply a filter or anything that removes some items without resetting the indexes. Use ->values() after applying filter. Commented Feb 14, 2023 at 17:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.