Skip to content

Relations ​

HexaORM loads relations in exactly 2 queries using WHERE IN — never N+1.

Defining relations ​

php
class Airline extends Model
{
    public function flights(): HasMany
    {
        return $this->hasMany(Flight::class, 'airline_id');
    }

    public function hub(): HasOne
    {
        return $this->hasOne(Airport::class, 'airline_id');
    }

    public function country(): BelongsTo
    {
        return $this->belongsTo(Country::class, 'country_id');
    }

    public function passengers(): BelongsToMany
    {
        return $this->belongsToMany(Passenger::class, 'flight_passengers', 'airline_id', 'passenger_id');
    }
}

Available relations ​

MethodTypeDescription
hasOne(Model, fk)One-to-oneAirline → Hub
hasMany(Model, fk)One-to-manyAirline → Flights
belongsTo(Model, fk)InverseFlight → Airline
belongsToMany(Model, pivot, fk, rfk)Many-to-manyFlight ↔ Passengers
hasOneThrough(Model, Through, fk1, fk2)ThroughCountry → Hub via Airline
hasManyThrough(Model, Through, fk1, fk2)ThroughCountry → Flights via Airline
morphOne(Model, name)Polymorphic onePost → Image
morphMany(Model, name)Polymorphic manyPost → Comments
morphTo()Polymorphic inverseComment → owner

Eager loading ​

Relations are loaded with with() — one extra WHERE IN query per relation, never N+1.

php
$airlines = Airline::query()->with(['flights', 'country'])->get();

foreach ($airlines as $airline) {
    echo $airline->country->name;          // BelongsTo / HasOne → model or null
    foreach ($airline->flights as $flight) { // HasMany / BelongsToMany / *Through / MorphMany → array
        echo $flight->origin . ' → ' . $flight->destination;
    }
}

// Nested relations with dot notation
$countries = Country::query()->with('airlines.flights')->get();

Pass several relations as an array

with('flights', 'country') only loads flights. Use with(['flights', 'country']).

Not supported yet

  • Lazy loading: $flight->airline is only set after with('airline'). There is no $model->load().
  • Eager-loading constraints: with(['flights' => fn ($q) => ...]) is accepted but the closure is currently ignored.

Many-to-many: attach / detach ​

php
$airline->passengers()->attach($airline->id, $passenger->id); // duplicates are ignored
$airline->passengers()->detach($airline->id, $passenger->id);

Polymorphic relations ​

php
// A Post and a Flight can both have Comments
class Comment extends Model
{
    public function commentable(): MorphTo
    {
        // Columns default to the method name: commentable_type / commentable_id
        return $this->morphTo();
    }
}

class Post extends Model
{
    public function comments(): MorphMany
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}

// Usage
$posts    = Post::query()->with('comments')->get();      // $post->comments → array
$comments = Comment::query()->with('commentable')->get(); // $comment->commentable → Post|Flight|null

*_type stores the owner's fully qualified class name. Only subclasses of Model are loaded, so a tampered *_type value can't instantiate arbitrary classes.

Released under the MIT License.