In a relational database management system, you know that database tables are often related to one another. For example, a company may have many employees or an order could be related to the user who placed it. Laravel eloquent makes managing and working with these relationships very convenient. Laravel provides many relationships and in this tutorial, we will see how we handle one to one or we can call this hasOne eloquent relationship.
In this Laravel 9 one to one relationship example tutorial, I am going to use the User and Phone concept. Suppose, A user may have one phone and a phone may have one user. Let's see how we can define Laravel's one to one relationship.
Eloquent relationships are defined as methods in your Eloquent model classes. We can use the hasOne()
method to define one to one relationships and the first argument passed to the hasOne
method is the name of the related model class.
Assume that one user may have one Phone. So we can define this relationship like this:
app/Models/User.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the phone associated with the user.
*/
public function phone()
{
// $this->hasOne(Model::class,'foreign_key','local_key');
// remember, foreign_key & local_key are optional
return $this->hasOne(Phone::class,'user_id','id');
}
}
Now we can fetch every user's phone information from the controller like:
App\Http\Controllers\TestController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index($id)
{
$user = User::find($id);
/**
* Get the phone associated with the user.
*/
$user->phone;
}
}
Defining Inverse Relationship of hasOne
Now we know how to define hasOne or one to one relationship. Now we will see how we can define the inverse relationship of hasOne. Let's define a relationship on the Phone
model that will let us access the user that owns his/her phone. We can define the inverse of a hasOne
relationship using the belongsTo
method like:
app/Models/Phone.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
Read also: Laravel 9 Throttle Rate Limiters Middleware Example
Here we can also pass fereign_key and local_key like below:
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo(User::class, 'foreign_key', 'owner_key');
}
Now we can also fetch user like:
App\Http\Controllers\TestController.php
<?php
namespace App\Http\Controllers;
use App\Models\Phone;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index($id)
{
$phone = Phone::find($id);
$phone->user;
}
}
Read also: Complete Explanation With Example On Laravel Middleware
Conclusion
Now we can know laravel 9 one to one relationship and how it works. Hope this Laravel 9 hasOne relationship tutorial will help you.