PHP/Laravel:嵌套对象变成数组



为了验证传入的请求数据,我使用validate方法。我必须创建不同的模型,所以我对不同的数据使用不同的验证规则。

我的传入数据看起来像这样

{
"type": "b2b",
"registration_number": "123456789",
"vat_number": "123456789",
"company_name": "Test company",
"legal_entity_type": "NV",
"phone": "1234567",
"email": "test@company.com",
"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": "john.doe@gmail.com"
},
{
"function": "COO",
"first_name": "Jane",
"last_name": "Doe",
"phone": "987654321",
"email": "jane.doe@gmail.com"
}
]
}

验证基本信息不是问题

$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'],
]);

但是嵌套的对象是。我想验证物理地址和账单地址,但php将这些对象转换为数组。这样做会导致"Call to a member function validate() on array"错误:

$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'],
]);

在Laravel文档中,我发现你可以使用点来验证嵌套数组数据

$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'],
]);

但是结果是一个只有一个元素的数组

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

为了进一步使用它,我必须将$physicalAddress的第一个元素分配给$physicalAddress:$physicalAddress = $physicalAddress[0]

因为这很简单,所以我不喜欢这额外的一行。对我来说,这是一种很粗糙的编码,我认为有一种更好的解决方案,首先将它作为一个对象。

有什么建议吗?

$physicalAddress = $request['physical_address']->validate()这不能简单地工作,因为validate()是一个请求方法和$request['physical_address']是一个数组。

使用验证有什么问题?

$validated = $request->validate([
'street' => 
'number' => 
'addition' => 
'zip' => 
'city' => 
'country' =>
'physical_address.street' =>
'physical_address.number' => 
'physical_address.addition' =>
'physical_address.zip' => 
'physical_address.city' => 
'physical_address.country' => 
'billing_address.street' => 
'billing_address.number' => 
'billing_address.addition' => 
'billing_address.zip' => 
'billing_address.city' => 
'billing_address.country' => 
'contacts.*.function' => 
'contacts.*.first_name' => 
'contacts.*.last_name' => 
'contacts.*.phone' => 
'contacts.*.email' => 
]);

最新更新