Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given

Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding the Argument 1 passed to create() must be of the type array, null given Error

The error message you are encountering is a classic symptom of a mismatch between what your controller expects and what Laravel's Model factory actually receives. Specifically, the stack trace points to Illuminate\Database\Eloquent\Model::create() being called with null instead of an array. In your specific case, this happens because the $request->all() or $request->only() method is returning null.

While it might seem logical that a POST request would always contain data, Laravel's Request object does not guarantee that the underlying input stream contains valid parameters in every scenario. If you attempt to call create(null), Laravel throws a TypeError immediately because the Eloquent builder requires an array of attributes to mass-assign.

Why $request->all() Returns Null

The root cause lies in how your controller handles the incoming data before passing it to the model. When using Route::post('articles', function(Request $request) { ... }), the closure receives a standard Laravel Illuminate\Http\Request instance.

If you inspect this request object, you will find that $request->all() returns an empty array [] if the input stream is empty or malformed, but in certain edge cases—specifically when the input stream is completely missing or the request body is not properly initialized—it can return null. This often happens during testing with tools that don't send a body, or in specific server configurations where the input stream handling differs from production.

The critical takeaway is that $request->all() is not strictly typed to return an array; it returns mixed. Passing this directly to Model::create() without validation creates a brittle endpoint that fails silently until a runtime type error occurs.

The Solution: Validation and Explicit Casting

To resolve this, you must ensure that the data passed to Article::create() is explicitly an array before the call executes. The most robust approach in Laravel is to use form request validation or explicit casting.

Option 1: Using Form Requests (Recommended)

The cleanest architectural pattern for REST APIs is to decouple validation logic from your controller. Create a custom form request class that ensures the title and body are present and cast them to arrays if necessary.

// app/Http/Requests/ArticleStoreRequest.php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ArticleStoreRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required|string|max:255',
            'body'  => 'required|string',
        ];
    }
}

Update your route to pass this request class to the controller, and then use the validated data within the controller method. The validated() method on the FormRequest instance guarantees an array structure:

// app/Http/Controllers/ArticleController.php
public function store(ArticleStoreRequest $request)
{
    // This will never be null because 'validated()' returns an array
    $article = Article::create($request->validated());

    return response()->json($article, 201);
}

Option 2: Explicit Casting in Controller

If you prefer to keep everything within the controller closure or do not want to create a separate form request class, you can explicitly cast the result of $request->all() to an array. This forces PHP to handle the conversion, ensuring that even if all() returns null, it becomes an empty array before reaching the database layer.

Route::post('articles', function(Request $request) {
    // Cast to array to prevent TypeError
    $data = (array) $request->all(); 
    
    // Alternatively, use only with a cast if filtering is needed
    // $data = (array) $request->only(['title', 'body']);

    if (empty($data)) {
        return response()->json(['message' => 'No data provided'], 400);
    }

    return Article::create($data);
});

Trade-offs and Best Practices

Using (array) casting is a defensive programming technique, but it has a cost: it masks validation logic. If a client sends {"title": "Article Name"} without a body, the array cast will succeed, but your database might reject the insert if body is mandatory in your schema.

Therefore, relying solely on casting is rarely sufficient for production APIs. You should always define explicit rules (via Form Requests or $request->validate()) to enforce data integrity. The error you encountered is a signal that the input stream handling was insufficient for your business logic; adding validation layers ensures that Article::create() receives a well-formed dataset every time, regardless of how the API client behaves.

Our network