Argument 1 passed to Illuminate\Validation\Factory::make() must be of the type array, object given
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Fixing the Type Mismatch: Why Validator::make() Fails with an Object Instead of an Array
As senior developers working with the Laravel ecosystem, we frequently encounter subtle but frustrating errors—errors that seem obscure but point to a fundamental misunderstanding of how data flows through the framework. One such common issue involves type mismatches when interacting with core components like validation.
Today, we are diving deep into a specific error: Argument 1 passed to Illuminate\Validation\Factory::make() must be of the type array, object given. This error often arises when attempting to use the Validator facade in Laravel, specifically when passing data that is structured as an object rather than the expected associative array.
Let’s break down why this happens in your specific scenario and how we can correct it using best practices.
Understanding the Validation Expectation
The core of the problem lies in what the Illuminate\Validation\Factory::make() method expects as its first argument. The validation system, at its heart, operates on structured data—specifically, an associative array where keys represent field names and values represent the input data (e.g., ['field_name' => 'input_value']).
When you call $validator = Validator::make($data, $rules), Laravel expects $data to be an array. If you pass an object instead, as in your example, Laravel throws this type error because it cannot iterate over the keys of a standard PHP object in the way it expects for input binding.
Analyzing Your Code Snippet
Let's examine the code provided:
public function store(Request $request)
{
$file = new File($request->all()); // Assuming this returns an object
$file->user_id = Auth::user()->id;
$validator = Validator::make($file->all(), [ // <-- The issue is here: passing the result of ->all()
'category_id' => ['bail', 'required'],
'titre' => ['bail', 'exclude_unless:category_id,1', 'required', 'string', 'min:3', 'max:255'],
'name' => ['bail', 'exclude_unless:category_id,2', 'required', 'string', 'min:3', 'max:255'],
'last' => ['bail', 'exclude_unless:category_id,1', 'required', 'string', 'min:3', 'max:255'],
]);
// ... rest of the code
}
In this context, the error occurs because $file->all() returns an object (or potentially a collection/object wrapper depending on how your File class is implemented), not a simple array of request parameters. To fix this, we must explicitly ensure that the data passed to the validator is a standard PHP array.
The Solution: Explicitly Casting to an Array
The solution is straightforward: before passing the data to Validator::make(), you must explicitly convert it into an array using PHP’s built-in functions, such as (array) or json_decode() if dealing with JSON strings.
In this case, since $file->all() is what you intend to validate, we should ensure that the data extracted from your file object is in the correct format. Assuming $file->all() returns an object that can be cast to an array of its contents, here is how you adjust the code:
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
public function store(Request $request)
{
// Step 1: Get the data intended for validation (assuming this is correct)
$dataToValidate = $file->all(); // This returns an object, causing the error
// Step 2: Explicitly cast the data to an array before validation
$validatedData = (array) $dataToValidate;
$validator = Validator::make($validatedData, [
'category_id' => ['bail', 'required'],
'titre' => ['bail', 'exclude_unless:category_id,1', 'required', 'string', 'min:3', 'max:255'],
'name' => ['bail', 'exclude_unless:category_id,2', 'required', 'string', 'min:3', 'max:255'],
'last' => ['bail', 'exclude_unless:category_id,1', 'required', 'string', 'min:3', 'max:255'],
]);
// ... proceed with file creation
}
Best Practice: Using Form Requests
While manually using Validator::make() is perfectly fine for simple operations, as you scale up your application, a more robust and Laravel-idiomatic approach is to utilize Form Requests.
Form Requests encapsulate all validation logic into dedicated classes, keeping your controller methods clean and adhering to the principles of separation of concerns. This is highly recommended when dealing with complex data structures or user input submitted via HTTP requests, aligning perfectly with how Laravel manages request lifecycle, as detailed on the Laravel documentation.
Conclusion
The error you encountered—object given—is a classic type mismatch issue stemming from expecting an array where an object was provided for validation. By explicitly casting your input data to an array using (array), you resolve this immediate problem. For long-term maintainability and adherence to Laravel best practices, always strive to use Form Requests for handling request validation, ensuring cleaner, more scalable code throughout your application.