foreach() argument must be of type array|object, null given", exception: "ErrorException

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Solving the "foreach() argument must be of type array|object, null given" Error with Laravel DataTables

As a senior developer working within the Laravel ecosystem, dealing with unexpected exceptions when integrating external packages is a common challenge. The error you are encountering—foreach() argument must be of type array|object, null given—is frustrating because it points to an issue deep within the library's execution, not necessarily in your primary database query.

This post will diagnose why you are seeing this error when trying to initialize DataTables with your Eloquent results and provide a robust solution. We will cover data handling best practices specific to Laravel Collections and external table libraries.


Diagnosing the Error: Why is null being passed?

The error message itself is quite clear: some function attempted to iterate over a variable that held the value null instead of an array or object, which is invalid.

You provided the following context:

  1. You fetch data using $dataLists = DB::table('table_name')->select(...)->get();. This returns a Illuminate\Support\Collection object.
  2. When you pass this collection to DataTables: Datatables::of($dataLists)->make(true);.

While the Collection object itself is an object, it's possible that the specific version or implementation of the DataTables package expects a raw, simple PHP array for its input, especially when dealing with collections returned from the database layer. When the library attempts an internal foreach loop on the collection structure, an unforeseen incompatibility occurs, leading to the null given error if the underlying data structure isn't precisely what it anticipates at that specific point in the rendering process.

The core issue is usually one of compatibility or strictness in how external libraries handle PHP data types versus Laravel’s native Collection objects.

The Solution: Ensuring Array Compatibility

The most reliable way to resolve this incompatibility is to explicitly convert your Laravel Collection into a standard, indexed PHP array before passing it to the DataTables facade. This guarantees that the input adheres strictly to the expected contract of the external library.

Here is the corrected approach for your controller logic:

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller; // Assuming this is a controller

class YourController extends Controller
{
    public function index()
    {
        // 1. Fetch data from the database
        $dataLists = DB::table('table_name')
                        ->select('column_1', 'column_2')
                        ->get(); // $dataLists is an Illuminate\Support\Collection

        // 2. Convert the Collection to a standard PHP array for compatibility
        $dataArray = $dataLists->toArray();

        // 3. Pass the plain array to the DataTables facade
        return Datatables::of($dataArray)
                    ->make(true);
    }
}

Why this works:

By calling $dataLists->toArray(), you transform the Laravel Collection object into a standard, indexed PHP array. This array is universally understood by almost all PHP functions and external libraries, including DataTables, eliminating the possibility of internal type mismatches that lead to the null given error.

Best Practices for Database Integration in Laravel

When building applications using Laravel, especially when integrating third-party front-end libraries like DataTables, maintaining strict data typing is crucial for stability. As we explore robust database interactions with tools like Eloquent and the Query Builder, ensuring that intermediate data structures are standardized prevents these types of runtime errors. For more insights into structuring clean data retrieval in Laravel, always refer to resources from laravelcompany.com.

Handling Null Results Safely

An additional best practice is defensive programming. If your database query might fail or return no results (which would result in an empty Collection), you should always check for existence before attempting to process the data.

You can enhance your code to handle potential null scenarios gracefully:

$dataLists = DB::table('table_name')
                ->select('column_1', 'column_2')
                ->get();

if ($dataLists) {
    $dataArray = $dataLists->toArray();
    return Datatables::of($dataArray)->make(true);
} else {
    // Handle the case where no data was returned (e.g., return an empty table structure or a specific error response)
    return response()->json(['message' => 'No data found'], 200);
}

Conclusion

The error you faced is a classic example of a mismatch between the expected input type of an external library and the actual object returned by the framework. By explicitly converting your Laravel Collection into a standard PHP array using the ->toArray() method, you resolve the conflict, ensuring compatibility and making your integration stable. Always prioritize data standardization when connecting Eloquent, the Query Builder, and third-party packages to build resilient applications on Laravel.