Master Laravel Eloquent database relationships like hasMany and belongsTo. Learn to link your models efficiently with this practical guide for PHP developers.
Last week, I watched a junior developer struggle for three hours trying to join three separate tables in raw SQL just to display a list of comments under a blog post. When I showed them how to handle it with a single line of code in Eloquent, the look of relief was priceless. You don’t need to write complex SQL joins to manage your data; that's exactly why we use Laravel.
At its core, Laravel Eloquent is an ORM (Object-Relational Mapper) that makes interacting with your database feel like working with standard PHP objects. When your application grows, your data inevitably needs to talk to other data. A User has Posts, a Post has Comments, and so on.
Before diving into the code, remember that Eloquent relies on naming conventions. If your table is named posts, Laravel assumes the model is named Post. If you follow these conventions, you’ll spend less time writing configuration and more time writing features. If you're new to the ecosystem, I highly recommend reviewing the Eloquent basics: models, relationships, and your first queries to ensure your model setup is solid.
The most common link you'll build is the one-to-many relationship. Think of a blog: one author can write many posts. In your database, this means the posts table has a user_id column pointing back to the users table.
To define this in your models, you use the hasMany and belongsTo methods.
In your User model, you define the relationship like this:
PHPnamespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { public function posts() { return $this->hasMany(Post::class); } }
In your Post model, you define the inverse:
PHPnamespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function user() { return $this->belongsTo(User::class); } }
Once these are set, you can access the data intuitively. If you have a $post object, you can simply call $post->user->name to get the author's name. It feels like magic, but it's just well-structured code.
We’ve all made the mistake of trying to manually join tables using DB::table() when a simple relationship would have sufficed. I remember a project where we manually joined four tables for a report; it was a maintenance nightmare. When we refactored it to use Eloquent relationships, we cut the code size by about 40% and made it much more readable.
One thing to watch out for is the "N+1 query problem." If you loop through 50 posts and call $post->user->name for each one, Laravel will execute 51 queries (1 for the posts, 50 for the users). Always use eager loading to fetch related data in one go:
PHP$posts = Post::with('user')->get();
By adding with('user'), Laravel fetches all associated authors in a single additional query. It’s a small change that saves a massive amount of database overhead.
Once you're comfortable with these basics, you'll want to keep your models clean. As your logic grows, you might find yourself adding complex constraints to these relationships. That’s when you should look into Mastering Laravel Eloquent Scopes: Writing Reusable Query Constraints to keep your controller logic slim.
If you are just getting started with a new project, don't forget to populate your database with dummy data to test these links. I always use Mastering Laravel Database Seeders: Create Realistic Testing Data to ensure my relationships hold up under real-world scenarios.
What if my foreign key isn't named user_id?
No problem. You can pass the custom key as the second argument: return $this->hasMany(Post::class, 'author_id');.
Does this work with other databases? Yes. Whether you are using MySQL, PostgreSQL, or SQLite, Eloquent abstracts the underlying SQL, so your relationship code stays the same.
When should I use hasOne instead of hasMany?
Use hasOne when a record strictly belongs to only one other record, like a User having one Profile.
I’m still learning new ways to optimize these queries every day. Sometimes, for high-traffic read models, I’ll even step outside of standard relationships to explore CQRS with Materialized Views: Scaling Laravel Read Models, but for 90% of your daily work, the standard Eloquent relationships are exactly what you need. Keep your models simple, trust the naming conventions, and don't be afraid to read the source code if you get stuck.
Eloquent basics are essential for every Laravel developer. Learn how to work with models, relationships, and queries to build cleaner database interactions.