Should I use belongsTo or hasOne?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Should I use `belongsTo` or `hasOne`? Demystifying Eloquent Relationships As developers working with Laravel and Eloquent ORM, understanding how to structure relationships correctly is fundamental. When dealing with one-to-one relationships between two models, say Model A and Model B, the choice between defining a relationship as `hasOne` or `belongsTo` can seem trivial. However, the decision dictates not just how your code looks, but more importantly, how Eloquent queries data, handles foreign keys, and maintains data integrity in your database schema. This post dives deep into the nuances of these two crucial relationship methods, focusing on a one-to-one scenario, to clarify when and why you should use each one. ## The Conceptual Difference: Ownership vs. Belonging The core difference between `hasOne` and `belongsTo` lies in defining the *direction* and *ownership* of the relationship from the perspective of the models involved. ### Understanding `hasOne` When you define a relationship as `$this->hasOne(ModelB::class)`, you are telling Eloquent that the current model (Model A) *possesses* or *has one* related instance of Model B. Conceptually, Model A is the "one" side in this pairing. In database terms, this relationship implies that Model A holds the foreign key pointing to Model B. For a one-to-one setup, this means Model A will contain the `b_id` column. ### Understanding `belongsTo` Conversely, `$this->belongsTo(ModelA::class)` defines the inverse relationship. This tells Eloquent that the current model (Model B) *belongs to* or is associated with a single instance of Model A. This side requires the foreign key to exist on its own table. In essence, `hasOne` describes the "parent" or "owner," while `belongsTo` describes the "child" or the entity that references the parent. ## Practical Implementation: Code Examples Let's assume we have two models: `User` (Model A) and `Profile` (Model B), where every user has exactly one profile. ### 1. Defining the Relationship in Models For a true one-to-one relationship, typically only one direction needs to be explicitly defined on both sides, but defining both ensures Eloquent knows how to load data efficiently. **In the `User` Model (The "One" side):** ```php // app/Models/User.php class User extends Authenticatable { // User has one Profile public function profile() { return $this->hasOne(Profile::class); } } ``` **In the `Profile` Model (The "Belongs To" side):** ```php // app/Models/Profile.php class Profile extends Model { // Profile belongs to one User public function user() { return $this->belongsTo(User::class); } } ``` ### 2. Database Structure (Migrations) For this setup, the foreign key must reside on the table that doesn't have a one-to-one relationship defined by the primary ownership convention. In many one-to-one scenarios, it is cleanest to place the foreign key on the dependent model: ```php // Migration for profiles table Schema::create('profiles', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained()->onDelete('cascade'); // FK pointing to users table $table->string('bio'); $table->timestamps(); }); // Migration for users table (no extra fields needed here for the 1:1 link) Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); // ... other user fields }); ``` ## Can I Use Them Interchangeably? **No, you cannot use them interchangeably.** While they describe the same underlying connection in a one-to-one context, their usage is semantically critical for Eloquent's functionality: 1. **Querying:** If you start from the `User` model, you use `$user->profile` (driven by `hasOne`). If you start from the `Profile` model, you must use `$profile->user` (driven by `belongsTo`) to fetch the related user data. 2. **Eager Loading:** When eager loading relationships using methods like `with()`, Eloquent uses these defined relationships to construct efficient SQL joins or separate queries necessary to retrieve the correct associated data. Mixing them up will lead to runtime errors or incorrect results because Eloquent relies on these explicit definitions to map code to the physical structure of your database schema. Always define the relationship based on which model *owns* the foreign key in your database design. ## Conclusion For a one-to-one relationship between Model A and Model B, always use **`hasOne`** on the side that dictates ownership (the parent) and **`belongsTo`** on the side