Relationships

Contents
HasMany
BelongsTo
Eager loading
Save related

HasMany

A post owns many comments, which is a one-to-many link. The method name becomes the property you read, so fetching a post and its comments reads naturally.

The return type tells readers and tools what the method gives back. The related class is named, never spelled out with keys.

namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; class Post extends Model { public function comments(): HasMany { return $this->hasMany(Comment::class); } }

BelongsTo

The other side of the link is a comment that belongs to one post. BelongsTo is the mirror image of HasMany, read from the child up to the parent.

namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; class Comment extends Model { public function post(): BelongsTo { return $this->belongsTo(Post::class); } }

Eager loading

Reading comments inside a loop can fire one query per post, which slows pages down fast. The with method preloads the relation in a single extra query.

Eager load every relation the page prints. Lazy loading stays fine for single models outside loops.

$posts = Post::with("comments")->get(); foreach ($posts as $post) { echo count($post->comments); }

Save related

New children can be created through the relation itself. Laravel fills the foreign key from the parent, so you never set it by hand.

$post = Post::find(1); $post->comments()->create(["body" => "Nice post!"]);

Next: Query builder

Article author: Arthur Isaev

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: