F
Filament14mo ago
Dakaa

Is there a Select field for saving morphToMany relationship?

using this relationship: https://laravel.com/docs/10.x/eloquent-relationships#many-to-many-polymorphic-relations schemas defined:
Schema::create('ad_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
});

Schema::create('ad_categorisables', function (Blueprint $table) {
$table->foreignId('ad_category_id');
$table->integer('ad_categorisable_id');
$table->string('ad_categorisable_type');
});

Schema::create('magazine_ads', function (Blueprint $table) {
$table->id();
$table->string('name');
});
Schema::create('ad_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
});

Schema::create('ad_categorisables', function (Blueprint $table) {
$table->foreignId('ad_category_id');
$table->integer('ad_categorisable_id');
$table->string('ad_categorisable_type');
});

Schema::create('magazine_ads', function (Blueprint $table) {
$table->id();
$table->string('name');
});
models defined:
class AdCategory extends BaseModel
{
public function magazineAds(): MorphToMany
{
return $this->morphedByMany(MagazineAd::class, 'ad_categorisable');
}
}

class MagazineAd
{
public function adCategories(): MorphToMany
{
return $this->morphToMany(AdCategory::class, 'ad_categorisable');
}
}
class AdCategory extends BaseModel
{
public function magazineAds(): MorphToMany
{
return $this->morphedByMany(MagazineAd::class, 'ad_categorisable');
}
}

class MagazineAd
{
public function adCategories(): MorphToMany
{
return $this->morphToMany(AdCategory::class, 'ad_categorisable');
}
}
On MagazineAdResource, I want to attach one AdCategory to MagazineAd without using Relation Manager:
$magazineAd = MagazineAd::first(); // example
$adCategory = AdCategory::first();; // example


$magazineAd->adCategories()->attach($adCategory->id); // trying to achieve this part
$magazineAd = MagazineAd::first(); // example
$adCategory = AdCategory::first();; // example


$magazineAd->adCategories()->attach($adCategory->id); // trying to achieve this part
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server