Can a Relation manager use get actions?

I have a Resource for category and a relation for posts in that category, I want to create a generate button sort of like create button on the top right hand side. I have this in my PostRelationsManager.php
protected function getActions(): array
{
$actions[] = CreateAction::class::make();
$actions[] = Action::make('Generate')
->color('danger')
->requiresConfirmation()
->icon('heroicon-o-document-search')
->action('generatePost');

return $actions;
}
protected function getActions(): array
{
$actions[] = CreateAction::class::make();
$actions[] = Action::make('Generate')
->color('danger')
->requiresConfirmation()
->icon('heroicon-o-document-search')
->action('generatePost');

return $actions;
}
8 Replies
Patrick Boivin
I think this should work... are you running into any issues? Sorry, I went too fast... you need to add your actions in table(), like a regular resource
->headerActions([
// ...
])
->headerActions([
// ...
])
datarecall
datarecallOP2y ago
ahh so that goes in the table part of it, let me give that a try how would you go about getting the ID of the category that is currently being viewed
Patrick Boivin
$this->ownerRecord
datarecall
datarecallOP2y ago
that seemed to do the trick thank you @pboivin @pboivin one last question I have this action on My List Posts resource
protected function getActions(): array
{
$actions[] = CreateAction::class::make();
$actions[] = Action::make('Generate')
->color('danger')
->requiresConfirmation()
->icon('heroicon-o-document-search')
->form([
Select::make('categoryId')
->label('Category')
->options(Category::query()->pluck('name', 'id'))
->required(),
])
->action('generatePost');

return $actions;
}
public function generatePost($id): void
{
dd($id);
CreateBlogPostForCategory::dispatch($this->ownerRecord);
}
protected function getActions(): array
{
$actions[] = CreateAction::class::make();
$actions[] = Action::make('Generate')
->color('danger')
->requiresConfirmation()
->icon('heroicon-o-document-search')
->form([
Select::make('categoryId')
->label('Category')
->options(Category::query()->pluck('name', 'id'))
->required(),
])
->action('generatePost');

return $actions;
}
public function generatePost($id): void
{
dd($id);
CreateBlogPostForCategory::dispatch($this->ownerRecord);
}
How can I pass the id or model into the generate post method
Patrick Boivin
Which id are you thinking about? The record for the current page?
datarecall
datarecallOP2y ago
This action has a form where they select the category, need to get the category ID into generatePost method
Patrick Boivin
Ah, I see! Something like this
->action(function ($data, $livewire) {
$livewire->generatePost($data['categoryId']);
})
->action(function ($data, $livewire) {
$livewire->generatePost($data['categoryId']);
})
datarecall
datarecallOP2y ago
ahhh perfect thank you

Did you find this page helpful?