Level 56
Is it required to have a single 'phone_type=0' in the request payload, or also in the database when saving it?
Also, how are you saving these phone numbers into the database?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi everyone, I am trying to do validation for array of phones, and there is a field phone_type which can be 0 or 1. According to business logic, there can be only one phone with type 0 in array and multiple with type 1. Did anybody face that kind of issue?
try this on a Form Request:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SampleRequest extends FormRequest
{
public function rules()
{
return [
'phones' => [
'array',
function ($attribute, $value, $fail) {
if (collect($value)->where('phone_type', 0)->count() > 1) {
$fail('There should be at most one phone number with type 0 ');
}
}
],
];
}
}
Reference: https://laravel.com/docs/8.x/validation#using-closures
Please or to participate in this conversation.