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
- Eloquent Models and Migrations
- CRUD with Eloquent
- Eloquent Relationships
- Eager Loading and Query Scopes