0Pricing
PHP Academy · Lesson

Eloquent Relationships

Define hasOne, hasMany, belongsTo, and many-to-many relationships.

Eloquent Relationships is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Relationships?

Eloquent relationships let you query related models using expressive PHP instead of raw SQL JOINs.

hasOne

A User has one Profile. Define on the owning model.

<?php
class User extends Model {
    public function profile(): \Illuminate\Database\Eloquent\Relations\HasOne {
        return $this->hasOne(Profile::class);
    }
}
// Access:
$profile = $user->profile;

belongsTo

A Profile belongs to a User. Define on the model that holds the foreign key.

<?php
class Profile extends Model {
    public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo {
        return $this->belongsTo(User::class);
    }
}
// Access:
$user = $profile->user;

hasMany

A User has many Posts. One-to-many from the parent side.

<?php
class User extends Model {
    public function posts(): \Illuminate\Database\Eloquent\Relations\HasMany {
        return $this->hasMany(Post::class);
    }
}
// Access:
$posts = $user->posts; // Collection

belongsTo (many side)

Each Post belongs to a User.

<?php
class Post extends Model {
    public function author(): \Illuminate\Database\Eloquent\Relations\BelongsTo {
        return $this->belongsTo(User::class, "user_id");
    }
}

Many-to-Many (belongsToMany)

A Post has many Tags, and a Tag belongs to many Posts — linked through a pivot table.

<?php
class Post extends Model {
    public function tags() {
        return $this->belongsToMany(Tag::class);
    }
}
// Pivot table: post_tag (post_id, tag_id)
$tags = $post->tags;
$post->tags()->attach($tagId);
$post->tags()->detach($tagId);
$post->tags()->sync([1, 2, 3]);

hasManyThrough

Access distantly related models: a Country has many Posts through Users.

<?php
class Country extends Model {
    public function posts() {
        return $this->hasManyThrough(Post::class, User::class);
    }
}

Polymorphic Relations

A Comment model that can belong to either a Post or a Video using polymorphic relations.

<?php
class Comment extends Model {
    public function commentable() {
        return $this->morphTo();
    }
}
class Post extends Model {
    public function comments() {
        return $this->morphMany(Comment::class, "commentable");
    }
}

Relationship Existence Queries

Query models based on the existence of a relationship.

<?php
// Posts that have at least one comment:
$commented = Post::has("comments")->get();
// Posts with >= 3 comments:
$popular = Post::has("comments", ">=", 3)->get();

whereHas

Filter based on relationship conditions.

<?php
$phpPosts = Post::whereHas("tags", function ($q) {
    $q->where("name", "PHP");
})->get();

Relationship Methods vs Dynamic Properties

$user->posts() returns the query builder (you can chain). $user->posts returns the loaded Collection.

Summary

Define relationships as methods returning hasOne, hasMany, belongsTo, belongsToMany. Access them as dynamic properties or chain queries off the method call.

Quick Check

Which relationship type uses a pivot table?

Frequently asked questions

Is the “Eloquent Relationships” lesson free?

Yes — the full text of “Eloquent Relationships” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Eloquent Relationships”?

Define hasOne, hasMany, belongsTo, and many-to-many relationships. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Eloquent Relationships” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Eloquent Models and Migrations
  2. CRUD with Eloquent
  3. Eloquent Relationships
  4. Eager Loading and Query Scopes
← Back to PHP Academy