LARAVEL Argument 1 passed to ... Must be of the type array, int given
This error message is a classic symptom of a mismatch between how you are passing data and what Laravel's query builder expects. When you see Illuminate\Database\Grammar::parameterize() complaining that it received an integer instead of an array, the root cause is almost always in how you are handling the return value of request()->validate().
In your initial working example, you used compact('description'). This function takes the variables currently in scope and creates an associative array containing them. Since $description was defined by request()->validate(), it became a local variable within that specific line. The resulting array structure looks like ['description' => 'some value'], which is exactly what the database grammar needs to bind parameters safely against SQL injection risks.
When you switched to assigning the validation result directly to $attributes and passing that object, the behavior changed because of how request()->validate() operates under the hood. According to Laravel's documentation, this method returns an array only if the validation passes. However, it returns a boolean false if validation fails.
The critical detail here is that in your code snippet, you are not checking the result before passing it. If the validation logic inside addTask triggers a failureâeven for a seemingly simple field like a required stringâthe method returns false. When $attributes holds the value false, PHP treats this as an integer (0) in many contexts. Consequently, when your addTask method attempts to pass this single boolean/integer value into the database query builder via compact('description'), the logic breaks down. The builder expects an array of key-value pairs to map to columns, but it receives a scalar value.
The error trace points specifically to Grammar.php line 869, where Laravel attempts to parameterize the query arguments. It finds an integer (representing the failed boolean) where it expects an array of bindings.
To resolve this, you must ensure that $attributes is always treated as an associative array before being used for database operations. There are three robust patterns to handle this:
Validate and Extract: If you use
request()->validate(), immediately capture the result into a variable and check it. If validation fails, return early or redirect.$attributes = request()->validate(['description' => 'required']); if ($attributes === false) { // Handle validation error return redirect()->back()->withErrors($attributes); } $this->tasks()->create($attributes);Use
onlyorexcept: If you prefer not to assign the whole result, pass the specific key directly:$project->addTask(request()->validate(['description' => 'required'])->get('description'));Note: This requires modifying your model's
addTaskmethod signature to accept a string rather than an array.Use the Request Object Directly: Bypass the validation call's return type ambiguity by accessing the validated data directly from the request instance after validation passes:
$validated = request()->validate(['description' => 'required']); $project->addTask($validated); // Ensure addTask handles array inputs correctly
The most common pitfall in this scenario is assuming request()->validate() always returns an array. While convenient, it introduces a boolean return type that can silently convert to an integer if not guarded. Always guard against the false return value when using this specific API. If you find yourself frequently dealing with validation results that might fail, consider using request()->only(['description']) combined with manual checks, or relying on the form request class which provides clearer separation of concerns and consistent array returns upon successful handling.