Block Repeater Rating with MorphToMany Relation
Greetings,
I have a block with this form inside
class Rating
{
public static function make(
string $name = 'rating',
string $context = 'form',
):Block {
$views = [$view1, $viev2];
return Block::make($name)
->schema([
Select::make('_tpl')
->label('layout')
->options($views),
Repeater::make('ratings')
->relationship()
->schema([
TextInput::make('title')->required(),
ColorPicker::make('color'),
SpatieMediaLibraryFileUpload::make('rating')
->collection('rating'),
])
->reorderableWithButtons()
->reorderableWithDragAndDrop(true)
->columnSpanFull()
->columns(3),
])
// ->reorderableWithButtons()
// ->addActionLabel('Add member')
// ->label('Link to article')
->columns('form' === $context ? 2 : 1);
}
}
used inside my ArticleResource resource, whose model has the relationship
public function ratings(): MorphToMany
{
$pivot_class = RatingMorph::class;
$pivot = app($pivot_class);
$pivot_table = $pivot->getTable();
$pivot_db_name = $pivot->getConnection()->getDatabaseName();
$pivot_table_full = $pivot_db_name.'.'.$pivot_table;
$pivot_fields = $pivot->getFillable();
return $this->morphToMany(Rating::class, 'model', $pivot_table_full)
->using($pivot_class)
->withPivot($pivot_fields)
->withTimestamps();
}
when I do a save, everything is fine, but:
- if I delete a rating, nothing inside the tables is deleted
- if I import data via xls/json, when I view the edit page, the data is not displayed/loaded inside
where am I wrong? any suggestions?
Thanks in advance
0 Replies