Get array of Eloquent model's relations

Get array of Eloquent model's relations

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

Stefan Bogdanescu

Founder & Senior Architect

Get array of Eloquent model's relations

When you inspect an Eloquent model instance in a debugger or via dd(), it might seem like the $relations property is your gateway to discovering defined associations. You see methods like author() and category() declared in your class, yet accessing $model->relations yields an empty array. This discrepancy isn't a bug; it's a deliberate architectural choice in how Laravel manages lazy loading and hydration.

The $relations attribute is not a static registry of all possible relations defined on the model. Instead, it is a dynamic container that gets populated only after you have explicitly accessed those relations or performed a query that hydrates them into the instance. If you instantiate an Article model without querying for its data first, or if you access it before calling any relationship methods, the internal $relations array remains empty. This design prevents unnecessary database hits and keeps memory usage low until the application actually needs those specific pieces of data.

To programmatically retrieve a list of all relation names defined on a model class without relying on the runtime state of an instance, you must inspect the model's definition directly. The most robust approach involves checking which methods in the model return a relationship instance. Since Eloquent relations are strictly typed as returning objects implementing the Relations interface, you can iterate through the model's public methods and filter them based on their return type or runtime class check.

Here is a practical implementation pattern to achieve this:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

public static function getRelationNames(Model $model): array
{
    $relations = [];
    
    foreach ($model->getMethods() as $methodName) {
        // Skip magic methods and internal Laravel methods
        if (str_starts_with($methodName, 'get') || in_array($methodName, ['toArray', 'boot'])) {
            continue;
        }

        $method = new ReflectionMethod($model, $methodName);
        
        // Check if the method is public and returns a Relation
        if ($method->isPublic() && $method->getReturnType()) {
            $returnType = (string) $method->getReturnType();
            if (str_contains($returnType, 'Relation')) {
                $relations[] = $methodName;
            }
        }
    }

    return $relations;
}

This approach leverages PHP's reflection capabilities to scan the model class signature rather than its runtime data. By checking the return type hint of each public method, you can accurately identify belongsTo, hasMany, or other relation definitions regardless of whether the instance has been queried yet. This technique ensures that even if $model->relations is empty during initialization, your list remains accurate because it reflects the model's schema definition.

Alternatively, if you are working within a specific Laravel version where reflection overhead is a concern, you might prefer to manually maintain a constant array or utilize a trait that exposes relation names as properties. However, for dynamic applications where models evolve frequently, the reflection-based method offers the best balance between automation and performance. It respects the framework's lazy-loading philosophy while giving you the introspection power you need for generating API documentation, schema validation rules, or automated testing fixtures.

Remember that this list represents potential relationships, not necessarily populated data. If your application logic requires knowing which relations are actually loaded into memory at a specific moment, you would still need to interact with the $relations property after hydration. But for structural analysis and metadata extraction, inspecting the class definition provides the definitive list of available associations.

Our network