0PricingLogin
PHP Academy · Lesson

Eloquent Relationships

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

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;

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