Kritim Yantra
Apr 06, 2025
In Laravel 12, database relationships help define how different models (tables) interact with each other. A one-to-one relationship is the simplest type, where one record in a table is linked to exactly one record in another table.
In this blog, we’ll cover:
Let’s dive in!
A one-to-one (1:1) relationship means:
users
table → id
, name
, email
profiles
table → id
, user_id
, bio
, phone
Here, one user has one profile, and one profile belongs to one user.
composer create-project laravel/laravel one-to-one-demo
cd one-to-one-demo
Edit .env
file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_one_to_one
DB_USERNAME=root
DB_PASSWORD=
users
Table (Default in Laravel)Laravel already provides a users
migration (database/migrations/xxxx_create_users_table.php
).
profiles
TableRun:
php artisan make:migration create_profiles_table
Edit the migration:
// database/migrations/xxxx_create_profiles_table.php
Schema::create('profiles', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id'); // Foreign key
$table->string('bio')->nullable();
$table->string('phone')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Run migrations:
php artisan migrate
Laravel already has a User
model (app/Models/User.php
).
Now, create a Profile
model:
php artisan make:model Profile
// app/Models/Profile.php
protected $fillable = ['user_id', 'bio', 'phone'];
In User.php
:
public function profile()
{
return $this->hasOne(Profile::class);
}
hasOne()
defines the 1:1 relationship.In Profile.php
:
public function user()
{
return $this->belongsTo(User::class);
}
belongsTo()
defines the inverse relationship.use App\Models\User;
use App\Models\Profile;
// Create a User
$user = User::create([
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => bcrypt('password'),
]);
// Create a Profile for the User
$user->profile()->create([
'bio' => 'Web Developer',
'phone' => '1234567890',
]);
$user = User::find(1);
$profile = $user->profile; // Gets the profile
echo $profile->bio; // Output: "Web Developer"
$profile = Profile::find(1);
$user = $profile->user; // Gets the user
echo $user->name; // Output: "John Doe"
If you fetch multiple users and their profiles, avoid the N+1 problem using with()
:
// Without Eager Loading (Runs N+1 queries)
$users = User::all();
foreach ($users as $user) {
echo $user->profile->bio; // Runs a query for each user
}
// With Eager Loading (Runs 2 queries only)
$users = User::with('profile')->get();
foreach ($users as $user) {
echo $user->profile->bio; // No extra queries
}
// routes/web.php
Route::get('/user-profile', function () {
$user = User::with('profile')->first();
return view('user-profile', compact('user'));
});
<!-- resources/views/user-profile.blade.php -->
<h1>{{ $user->name }}</h1>
<p>Bio: {{ $user->profile->bio ?? 'No bio' }}</p>
<p>Phone: {{ $user->profile->phone ?? 'No phone' }}</p>
Run:
php artisan serve
Visit:
http://127.0.0.1:8000/user-profile
You’ll see the user’s profile details!
✅ Key Takeaways:
hasOne()
& belongsTo()
→ Define the relationship in models. with()
) → Optimizes database queries. 🚀 Happy Coding!
📌 Need help? Drop your questions below! 👇
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 21, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google