Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

khaledmsm's avatar

Laravel validation excludes specific fields (facilities and additional_facilities) from request data

0

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):

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)

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.

0 likes
1 reply
Nakov's avatar

And did you tried to dump your input data before the rules are applied? I see why the additional facilities might be missing, since you use nullable for that field, but for facilities you might not be passing any data.

So you can add this method in your form request:

protected function prepareForValidation(): void
{
        $this->mergeIfMissing([
            'facilities'            => [],
            'additional_facilities' => [],
        ]);
}

so if those fields are missing completely, they will be appended to the request. But I'll also suggest:

protected function prepareForValidation(): void
{
        $this->dd(); // this line will show you everything from the request that will be validated.
        
        ...
}

Please or to participate in this conversation.