Validation excludes specific fields from request data

I’m working on a Laravel project where I’m handling property creation, and I’m facing an issue where specific fields (facilities and additional_facilities) are missing from the validated data even though they are present in the request.

In my controller’s store() method, I use PropertyStoreRequest for validation. The request contains fields such as facilities, additional_facilities, amenities, etc.

After validation, I expect these fields to be present in the $validatedData, but for some reason, only the amenities are included, while facilities and additional_facilities are missing.

Here’s the relevant code:

Request Validation (PropertyStoreRequest):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class PropertyStoreRequest extends FormRequest
{
public function authorize(): bool
{
return auth()->user()->isHost();
}
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'description' => 'required|string',
'location' => 'required|string|max:255',
'price_per_night' => 'required|numeric|min:0',
'max_guests' => 'required|integer|min:1',
'photos' => 'required|array|min:3',
'photos.*' => 'image|mimes:jpg,jpeg,png|max:2048',
'facilities' => 'required|array',
'facilities.*' => 'integer|exists:facilities,id',
'additional_facilities' => 'nullable|array',
'additional_facilities.*' => 'integer|exists:additional_facilities,id',
'amenities' => 'required|array',
'amenities.*' => 'integer|exists:amenities,id',
];
}
protected function failedValidation(Validator $validator)
{
Log::error('Validation failed:', $validator->errors()->all());
throw new HttpResponseException(response()->json([
'errors' => $validator->errors(),
], 422));
}
}
</code>
<code>class PropertyStoreRequest extends FormRequest { public function authorize(): bool { return auth()->user()->isHost(); } public function rules(): array { return [ 'title' => 'required|string|max:255', 'description' => 'required|string', 'location' => 'required|string|max:255', 'price_per_night' => 'required|numeric|min:0', 'max_guests' => 'required|integer|min:1', 'photos' => 'required|array|min:3', 'photos.*' => 'image|mimes:jpg,jpeg,png|max:2048', 'facilities' => 'required|array', 'facilities.*' => 'integer|exists:facilities,id', 'additional_facilities' => 'nullable|array', 'additional_facilities.*' => 'integer|exists:additional_facilities,id', 'amenities' => 'required|array', 'amenities.*' => 'integer|exists:amenities,id', ]; } protected function failedValidation(Validator $validator) { Log::error('Validation failed:', $validator->errors()->all()); throw new HttpResponseException(response()->json([ 'errors' => $validator->errors(), ], 422)); } } </code>
class PropertyStoreRequest extends FormRequest
{
    public function authorize(): bool
    {
        return auth()->user()->isHost();
    }

    public function rules(): array
    {
        return [
            'title' => 'required|string|max:255',
            'description' => 'required|string',
            'location' => 'required|string|max:255',
            'price_per_night' => 'required|numeric|min:0',
            'max_guests' => 'required|integer|min:1',
            'photos' => 'required|array|min:3',
            'photos.*' => 'image|mimes:jpg,jpeg,png|max:2048',
            'facilities' => 'required|array',
            'facilities.*' => 'integer|exists:facilities,id',
            'additional_facilities' => 'nullable|array',
            'additional_facilities.*' => 'integer|exists:additional_facilities,id',
            'amenities' => 'required|array',
            'amenities.*' => 'integer|exists:amenities,id',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        Log::error('Validation failed:', $validator->errors()->all());
        throw new HttpResponseException(response()->json([
            'errors' => $validator->errors(),
        ], 422));
    }
} 

Controller (store() method)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function store(PropertyStoreRequest $request)
{
Log::info('Property coming data:', $request->all());
$validatedData = $request->validated(); // Facilities and additional_facilities missing
// Create property
$property = Property::create([
'title' => $validatedData['title'],
'description' => $validatedData['description'],
'location' => $validatedData['location'],
'price_per_night' => $validatedData['price_per_night'],
'max_guests' => $validatedData['max_guests'],
'photos' => json_encode($this->handleFileUpload($request, 'photos', 'property_photos')),
'user_id' => auth()->id(),
'is_available' => $validatedData['is_available'] ?? true,
]);
// Syncing relations
$this->syncRelations($property, $validatedData, $request);
return response()->json($property, 201);
</code>
<code>public function store(PropertyStoreRequest $request) { Log::info('Property coming data:', $request->all()); $validatedData = $request->validated(); // Facilities and additional_facilities missing // Create property $property = Property::create([ 'title' => $validatedData['title'], 'description' => $validatedData['description'], 'location' => $validatedData['location'], 'price_per_night' => $validatedData['price_per_night'], 'max_guests' => $validatedData['max_guests'], 'photos' => json_encode($this->handleFileUpload($request, 'photos', 'property_photos')), 'user_id' => auth()->id(), 'is_available' => $validatedData['is_available'] ?? true, ]); // Syncing relations $this->syncRelations($property, $validatedData, $request); return response()->json($property, 201); </code>
public function store(PropertyStoreRequest $request)
{
    Log::info('Property coming data:', $request->all());

    $validatedData = $request->validated(); // Facilities and additional_facilities missing

    // Create property
    $property = Property::create([
        'title' => $validatedData['title'],
        'description' => $validatedData['description'],
        'location' => $validatedData['location'],
        'price_per_night' => $validatedData['price_per_night'],
        'max_guests' => $validatedData['max_guests'],
        'photos' => json_encode($this->handleFileUpload($request, 'photos', 'property_photos')),
        'user_id' => auth()->id(),
        'is_available' => $validatedData['is_available'] ?? true,
    ]);

    // Syncing relations
    $this->syncRelations($property, $validatedData, $request);

    return response()->json($property, 201);

Logging output shows that facilities and additional_facilities are present in the raw request but not in $validatedData:

[2024-10-24 13:45:01] local.INFO: Property coming data: {…}

[2024-10-24 13:45:01] local.INFO: validated data from request : {… “amenities”:[“1″,”2”], “photos”:[…]}

[2024-10-24 13:45:01] local.INFO: Facilities to sync: []

[2024-10-24 13:45:01] local.INFO: Additional Facilities to sync: []

[2024-10-24 13:45:01] local.INFO: Amenities to sync: [1,2]

What I’ve tried:

  • Checked database existence: Both facilities and additional_facilities tables contain valid IDs that match the request.

  • Manually overriding data: I’ve manually set the facilities and additional_facilities in the controller after validation, and the relationships sync properly, which means there’s no issue with syncing—only with validation.

Logged the validation failure: No validation errors are logged, which suggests that validation for these fields is passing, but they still don’t appear in $validatedData.

3

It sounds like the issue may stem from the nullable rule and possibly the way the array values are being handled in the request data for facilities and additional_facilities.
so please try to make them required first and log them to check you are sending them correctly from postman.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật