Argument 1 passed, must be of the type array, string given
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Argument 1 passed must be of the type array, string given - Resolving Laravel 5.8 Error
Body: In this blog post, we will discuss the common error that occurs due to using the wrong data types while passing arguments in Laravel. This particular issue is commonly faced when working with arrays and strings within the framework. We'll learn how to resolve it by ensuring proper data types during method calls.
In your given code snippet, you have a method which selects all records from the database based on certain criteria: media files uploaded in a specific dimension, file_type, and file name. It returns these results as an array with two parts for better representation. But when you try to filter by the 'fileName' parameter, you encounter an error saying that Argument 1 passed must be of the type array, string given.
To understand the issue, let's break down your code and explain what is happening:
1. The 'fileName' search option is used in the query as a condition for filtering media files by name using `whereIn('med_name', request('fileName'))`. Here, the 'request('fileName')' part returns an array of file names if present in the URL or an empty array otherwise.
2. The issue arises when you use `$media` (representing all records) as the first argument for query clean bindings method `cleanBindings()`, which expects it to be of type array. Your code is now incorrectly doing a string comparison with an invalid data type, causing the error.
3. The problem stems from the fact that you've not validated your 'fileName' parameter prior to passing it into the query, leading to inconsistent data types in the process.
Now to fix this issue, we need to ensure proper data validation and correct usage of data types in the code:
1. In the method that handles searching by file name, replace `$media` with an empty array if 'fileName' isn't set or has no values: `$reqFileName = request('fileName') ? request('fileName') : [];`.
2. Create a separate condition for validating and filtering based on this modified 'fileName':
`if (!empty($reqFileName)) {
$media = $media->whereIn('med_name', $reqFileName);
}`
3. Call the cleaned array of records with the newly updated 'fileName' conditions: `$mediaQueryResults = $repoObject()->cleanBindings(collect([$media, $files]))->get();`
4. Return these results to your view as before with the modified data.
By following these steps and ensuring proper data validation and usage of data types in your code, you can avoid such errors and prevent any inconsistencies that may affect your application's functionality. Remember to always double-check your query conditions and verify the passed arguments for their correct types to ensure smooth operation of your Laravel application.