Add value to field in pivot table in CreateAction
Hello I have this code in v2
return $table
->columns(CategoryResource::getTableFields())
->filters([])
->headerActions([
Tables\Actions\CreateAction::make()
->using(function (HasRelationshipTable $livewire, array $data): Model {
$parentCategory = $livewire->getRelationship()->getParent();
$dataPivot['pivotorder'] = $parentCategory->subcategories()->max('pivotorder') + 1;
return $livewire->getRelationship()->create($data,$dataPivot);
}),
this code worked.
In v3
return $table
->recordTitleAttribute('name')
->columns(CategoryResource::getTableFields())
->filters([])
->headerActions([
CreateAction::make()
->using(function (array $data, string $model): Model {
return $model::create($data);
})
])
first, in headersActions never enters. but in emptyStateActions, it does.
second, how i can get the relationship for get the parent category?
thx
4 Replies
For your first question, try giving the CreateAction in emptyStateActions a name, like CreateAction::make('empty-create'), see if that allows the headerActions one to run.
Hello, yes, thanks if I remove the emptyStateActions, if enters in the using in headerActions
So where is this code? In a relation manager? Are you trying to get the "owner" record for the related table you are creating?
Yes, the code is in SubcategoriesRelationManager
I get it:
->headerActions([
CreateAction::make()
->using(function (array $data, string $model): Model {
$table = $this->getTable();
$relationship = $table->getRelationship();
$pivotData = [];
$pivotColumns = $relationship->getPivotColumns();
$pivotData = Arr::only($data, $pivotColumns);
$data = Arr::except($data, $pivotColumns);
$parentCategory = $relationship->getParent();
$pivotData['pivotorder'] = $relationship->max('pivotorder') + 1;
$record = new $model();
$record->fill($data);
$relationship->save($record, $pivotData);
return $record;
})
])
Now that I can debug it was easier π but surely there is a better way to do it than copy and paste π
Very thanks