Bunu “Model A → Model B → Model C” ilişkisi olarak düşünebilirsiniz:
Diyelim ki bir blog projesinde şöyle bir yapı var:
İlişkiler:
<?php
class Country extends Model
{
// Country üzerinden direkt Post'lara erişim
public function posts()
{
return $this->hasManyThrough(Post::class, User::class);
}
}
class User extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
public function country()
{
return $this->belongsTo(Country::class);
}
}
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
<?php
$country = Country::find(1);
$posts = $country->posts; // Ülkeye ait tüm kullanıcıların yazıları