Home:ALL Converter>PHP/Laravel: Nested object is turned into array

PHP/Laravel: Nested object is turned into array

Ask Time:2021-09-17T23:28:37         Author:Thore

Json Formatter

To validate incoming request data I'm using the validate method. I have to create different models so I'm using different validate rules with different data.

My incoming data looks like this

{
    "type": "b2b",
    "registration_number": "123456789",
    "vat_number": "123456789",
    "company_name": "Test company",
    "legal_entity_type": "NV",
    "phone": "1234567",
    "email": "[email protected]",
    "physical_address": {
        "street": "Teststreet",
        "number": "12",
        "addition": "5",
        "zip": "12345",
        "city": "Brussels",
        "country": "Belgium"
    },
    "billing_address": {
        "street": "Teststreet",
        "number": "12",
        "addition": "5",
        "zip": "12345",
        "city": "Brussels",
        "country": "Belgium"
    },
    "contacts": [
        {
            "function": "CEO",
            "first_name": "John",
            "last_name": "Doe",
            "phone": "123456789",
            "email": "[email protected]"
        },
        {
            "function": "COO",
            "first_name": "Jane",
            "last_name": "Doe",
            "phone": "987654321",
            "email": "[email protected]"
        }
    ]
}

Validating the basic information isn't a problem

$clientData = $request->validate([
    'type' => ['required', 'string', 'in:b2b,b2c'],
    'vat_number' => ['string', 'max:255'],
    'company_name' => ['string', 'max:255'],
    'legal_entity_type' => ['string', 'max:255'],
    'phone' => ['required', 'string', 'max:255'],
    'email' => ['required', 'string', 'max:255'],
    'first_name' => ['string', 'max:255'],
    'last_name' => ['string', 'max:255'],
    'date_of_birth' => ['date'],
]);

But the nested objects are. I want to validate the physical and billing address but php is turning these objects into arrays. Doing this leads to a "Call to a member function validate() on array" error:

$physicalAddress = $request['physical_address']->validate([
    'street' => ['required', 'string', 'max:255'],
    'number' => ['required', 'string', 'max:255'],
    'addition' => ['string', 'max:255'],
    'zip' => ['required', 'string', 'max:255'],
    'city' => ['required', 'string', 'max:255'],
    'country' => ['required', 'string', 'max:255'],
]);

In the Laravel docs I found out you can use a dot to validate nested array data

$physicalAddress = $request->validate([
    'physical_address.street' => ['required', 'string', 'max:255'],
    'physical_address.number' => ['required', 'string', 'max:255'],
    'physical_address.addition' => ['string', 'max:255'],
    'physical_address.zip' => ['required', 'string', 'max:255'],
    'physical_address.city' => ['required', 'string', 'max:255'],
    'physical_address.country' => ['required', 'string', 'max:255'],
]);

But the result is an array with one element

[2021-09-17 15:24:59] local.DEBUG: array (
  'physical_address' => 
  array (
    'street' => 'Teststreet',
    'number' => '12',
    'addition' => '5',
    'zip' => '12345',
    'city' => 'Brussels',
    'country' => 'Belgium',
  ),
)  

To use this further I have to assign the first element of $physicalAddress to $physicalAddress: $physicalAddress = $physicalAddress[0].

As this is quiet simple I don't like this extra line. It feels like hacky coding to me and I think there is like a better solution to keep it as an object in the first place.

Any suggestions?

Author:Thore,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69225933/php-laravel-nested-object-is-turned-into-array
yy