Laravel in_array validation rule

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Validating Array Membership with Laravel's in_array Rule Body:

Laravel is an incredibly powerful PHP framework that provides developers with a multitude of tools to efficiently and effectively build their applications. Validation is one such tool, which helps prevent and detect potential errors in user input data. Today, we will be focusing on using the Laravel validation rule called in_array. This rule is used specifically for validating array membership, thereby ensuring that a given input matches an element within a defined set of values.

The Problem with Validation in Our Scenario

Assume we have the following time slots available:

$this->allslots=array('10:00:00', '10:10:00', '10:20:00', '10:30:00', '10:40:00', '10:50:00', '11:00:00', '11:10:00', '11:20:00', '11:30:00', '11:40:00', '11:50:00', '12:00:00', '12:10:00', '12:20:00', '12:30:00', '12:40:00', '12:50:00', '13:00:00', '13:10:00', '13:20:00', '13:30:00', '13:40:00', '13:50:00', '14:00:00', '14:10:00', '14:20:00', '14:30:00', '14:40:00', '14:50:00', '15:00:00', '15:10:00', '15:20:00', '15:30:00', '15:40:00', '15:50:00', '16:00:00', '16:10:00', '16:20:00', '16:30:00', '16:40:00', '16:50:00');

The issue is that when we write the validation rule as follows:

$this->validate($request, [
        'field' => 'required|in_array:$this->allslots',
    ]);

This validation rule will return an error for every input. Why is that? Let us understand how the in_array validation works.

A Closer Look at Laravel's in_array Validation Rule

The Laravel in_array rule checks if a given input matches one of the values within an array. It is used to validate arrays. For instance, let us assume we have these valid values:

$validValues=array('a', 'b', 'c');

Now, suppose you want to ensure that the user input must match one of these values. You can use this validation rule as follows:

$this->validate($request, [
        'field' => 'required|in_array:$validValues',
    ]);

However, in our original problem where we have time slots within an array and want to validate that a given input matches one of the values, there is an issue. The in_array rule cannot be used directly with a normal array (non-associative or objects) as in this case. Instead, for it to work, you must convert your array of time slots into a collection or an associative array.

Possible Solutions

There are two ways to solve this problem:

1. Convert the original time slot array into a Laravel Collection: $collection=collect($allslots);. Then, you can use the in_array rule with the collection. 2. Transform your original array of strings into an associative array, where each string value is mapped to a specific key:
$associativeArray=[
    '10:00:00' => true,
    '10:10:00' => true,
    // ...
];
. Now you can use the validation rule as before.

In both cases, ensure that your input is a numeric key for better mapping and easier debugging. You could also consider using Laravel's collect or array_combine methods to automate this conversion process.

Conclusion

By learning from this example, you now understand the importance of correctly using Laravel's in_array validation rule and the best practices that ensure efficient and effective array membership validation. Always remember to convert your array into a collection or associative array before implementing the validation rules for accurate results.