0Pricing
PHP Academy · 课时

Eloquent 关系

定义 hasOne、hasMany、belongsTo 及多对多关系。

Eloquent 关系 是 CoddyKit 上的免费 PHP Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 PHP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 PHP Academy 课程共包含 4 节课。

为什么需要关系

Eloquent 关系让您可以使用表达力强的 PHP 查询相关模型,而不必直接编写 SQL JOIN。

hasOne

一个 User 拥有一个 Profile。在所属模型中定义此关系。

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

belongsTo

一个 Profile 属于一个 User。在持有外键的模型中定义此关系。

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

hasMany

一个 User 拥有多个 Post。这是从父级一侧定义的一对多关系。

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

belongsTo(多的一侧)

每个 Post 都属于一个 User。

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

多对多(belongsToMany)

一个 Post 拥有多个 Tag,一个 Tag 也属于多个 Post,它们通过中间表关联。

<?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

访问间接关联的模型:一个 Country 通过 User 拥有多个 Post。

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

多态关系

使用多态关系,使 Comment 模型可以属于 Post 或 Video。

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

关系存在性查询

根据关系是否存在来查询模型。

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

whereHas

根据关系条件筛选记录。

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

关系方法与动态属性

$user->posts() 返回查询构造器(您可以继续链接查询)。$user->posts 返回已加载的 Collection。

总结

将关系定义为返回 hasOne、hasMany、belongsTo 或 belongsToMany 的方法。您可以将它们作为动态属性访问,也可以在方法调用后继续链接查询。

快速检查

哪种关系类型使用中间表?

常见问题解答

「Eloquent 关系」课时是免费的吗?

是的 — 「Eloquent 关系」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 PHP Academy 课程的其余内容,请升级到 CoddyKit PRO。 PHP Academy 课程共包含 4 节课。

「Eloquent 关系」这节课中我会学到什么?

定义 hasOne、hasMany、belongsTo 及多对多关系。 你通过在浏览器中直接运行的动手代码来练习 PHP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 PHP Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 PHP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「Eloquent 关系」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 PHP Academy 课中编写并运行代码吗?

能。每节 PHP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Eloquent 模型与迁移
  2. 使用 Eloquent 实现 CRUD
  3. Eloquent 关系
  4. 预加载与查询作用域
← 返回 PHP Academy