I need help with my laravel 11 project. I have a table which acts as a morph table which would work with morphedByMany. But for this particular model I only need to retrieve one entry. My tables are: Site, Domain and then the intermidiate table: Siteable. Siteable defines the site_id, siteable_type and siteable_id columns. The following relationship defined at the Site model would retrieve all of my domains for the site, but a site should only have one domain:
public function domains(): MorphToMany
{
return $this->morphedByMany(Domain::class, 'siteable');
}
I tried to change this to:
public function domain(): HasOneThrough
{
return $this->hasOneThrough(Domain::class, Siteable::class, 'siteable_id', 'id', 'id');
}
Basically, a Site
can have one domain, and as many of other models as it chooses, thus the following schema:
Schema::create('siteables', function (Blueprint $table) {
$table->ulid('id')->primary()->index();
$table->foreignUlid('site_id')->index()->constrained()->onUpdate('cascade')->onDelete('cascade');
$table->ulidMorphs('siteable');
$table->timestamps();
// define indexes for performance
$table->index(['site_id', 'siteable_type', 'siteable_id']);
});
What am i missing to retrieve one record?