Laravel 11'de Eloquent İlişkileri: One to One, One to Many, Many to Many

Laravel’in güçlü ORM (Object-Relational Mapping) yapısı olan Eloquent, ilişkisel veritabanlarını kolayca yönetmemizi sağlar. Laravel 11’de Eloquent ilişkileri ile tablolar arasında bağlantılar kurabilir ve verileri daha düzenli bir şekilde çekebiliriz.
Bu rehberde One to One, One to Many ve Many to Many ilişkilerini nasıl kullanacağınızı detaylı şekilde inceleyeceğiz.
1. One to One (Birebir) İlişki
Birebir ilişki, bir tablonun başka bir tabloyla yalnızca tek bir satır üzerinden bağlantılı olduğu durumları ifade eder. Örneğin, bir kullanıcının yalnızca bir profili olabilir.
1.1. One to One İlişkisini Tanımlama
İlk olarak, users
ve profiles
tablolarını oluşturacağız. Her profilin bir kullanıcıya ait olduğunu belirtmek için profiles
tablosunda user_id
sütunu olmalıdır.
php artisan make:model Profile -m
database/migrations/xxxx_xx_xx_xxxxxx_create_profiles_table.php:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('profiles', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained()->onDelete('cascade'); $table->string('bio')->nullable(); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('profiles'); } };
1.2. Model İçinde One to One İlişkisini Tanımlama
app/Models/User.php:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasFactory; public function profile() { return $this->hasOne(Profile::class); } }
app/Models/Profile.php:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Profile extends Model { use HasFactory; public function user() { return $this->belongsTo(User::class); } }
1.3. One to One İlişkisini Kullanma
// Kullanıcının profilini getir $user = User::find(1); $profile = $user->profile; // Profilin kullanıcısını getir $profileOwner = Profile::find(1)->user;
2. One to Many (Bire Çok) İlişki
Bire çok ilişkide, bir tablonun birden fazla satırı başka bir tabloyla ilişkilendirilebilir. Örneğin, bir kullanıcının birden fazla gönderisi (post) olabilir.
2.1. One to Many İlişkisini Tanımlama
php artisan make:model Post -m
database/migrations/xxxx_xx_xx_xxxxxx_create_posts_table.php:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained()->onDelete('cascade'); $table->string('title'); $table->text('content'); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('posts'); } };
2.2. Model İçinde One to Many İlişkisini Tanımlama
app/Models/User.php:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasFactory; public function posts() { return $this->hasMany(Post::class); } }
app/Models/Post.php:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; public function user() { return $this->belongsTo(User::class); } }
2.3. One to Many İlişkisini Kullanma
// Kullanıcının tüm gönderilerini getir $user = User::find(1); $posts = $user->posts; // Bir gönderinin sahibini getir $postOwner = Post::find(1)->user;
3. Many to Many (Çoktan Çoğa) İlişki
Many to Many ilişkide, bir tablodaki birden fazla satır, başka bir tablodaki birden fazla satırla ilişkilendirilebilir. Örneğin, kullanıcılar birçok role sahip olabilir ve roller birçok kullanıcıya atanabilir.
3.1. Many to Many İlişkisini Tanımlama
php artisan make:model Role -m
database/migrations/xxxx_xx_xx_xxxxxx_create_roles_table.php:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('roles', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); Schema::create('role_user', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained()->onDelete('cascade'); $table->foreignId('role_id')->constrained()->onDelete('cascade'); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('role_user'); Schema::dropIfExists('roles'); } };
3.2. Model İçinde Many to Many İlişkisini Tanımlama
<?php class User extends Model { public function roles() { return $this->belongsToMany(Role::class); } } <?php class Role extends Model { public function users() { return $this->belongsToMany(User::class); } }
3.3. Many to Many İlişkisini Kullanma
// Kullanıcının rollerini getir $user = User::find(1); $roles = $user->roles; // Bir rolün kullanıcılarını getir $users = Role::find(1)->users;
Bu rehberde Laravel 11'de One to One, One to Many ve Many to Many ilişkilerini öğrendik. 🚀