z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

Polymorphic relation form #5647

Open NikolayMurha opened 2 years ago

NikolayMurha commented 2 years ago

Add polymorphic relation field support.

Usage example:

$form->radioButton('menuable_type', __('Link to'))->options([
    (new PageCategory())->getMorphClass() => __('Page Category'),
    (new Page())->getMorphClass() => __('Page'),
    (new Category())->getMorphClass() => __('Product Category'),
    (new Product())->getMorphClass() => __('Product'),
])->when((new PageCategory())->getMorphClass(), function (Form $form) {
    $form->morphTo('menuable', PageCategories::class, "Menu Object");
})->when((new Page())->getMorphClass(), function (Form $form) {
    $form->morphTo('menuable', Pages::class, "Menu Object");
})->when((new Category())->getMorphClass(), function (Form $form) {
    $form->morphTo('menuable', Categories::class, "Menu Object");
})->when((new Product())->getMorphClass(), function (Form $form) {
    $form->morphTo('menuable', Products::class, "Menu Object");
});