Creating custom livewire action Componente using relationships

Hey guys. I'm trying to create a Livewire component that will return an Action where I'm using
->fillForm(function ()
->fillForm(function ()
to fill a multi-select with tag options. It works perfectly when I'm using this action directly on my resource, using headerFunction from View page, but using a custom livewire component doesn't work. Taking a look I imagine that is something related with relationships. Blade: <livewire:manage-tag-action :model="$client"/> Component:
class ManageTagAction extends Component implements HasForms, HasActions
{
use InteractsWithForms;
use InteractsWithActions;

public Tag $tag;
public Model $model;

public function mount(Tag $tag, Model $model)
{
$this->tag = $tag;
$this->model = $model;
}
class ManageTagAction extends Component implements HasForms, HasActions
{
use InteractsWithForms;
use InteractsWithActions;

public Tag $tag;
public Model $model;

public function mount(Tag $tag, Model $model)
{
$this->tag = $tag;
$this->model = $model;
}
Solution:
You need to provide your model to the action. Probably ->record(fn ($livewire) => $livewire->model) or similar...
Jump to solution
4 Replies
leoblanski
leoblanski5mo ago
Continuing...
public function manageTagAction(): Action
{
return Action::make('manageTag')
->label('Manage Tags')
->fillForm(function () {
return $this->model->tags()->get()->toArray();
})
->form(function () {
$type = Str::singular($this->model->getTable());

return [
MultiSelect::make('tags')
->relationship('tags', 'name')
->createOptionForm(
[
TextInput::make('name'),
TextInput::make('description'),
]
)
->createOptionUsing(function(array $data) use ($type) {
$tag = Tag::create([
'name' => $data['name'],
'description' => $data['description'],
'category' => $type
]);

return $tag->id;
})
->searchable()
->options(
function () {
return Tag::query()->pluck('name', 'id');
}
)
->preload(),
];
})
public function manageTagAction(): Action
{
return Action::make('manageTag')
->label('Manage Tags')
->fillForm(function () {
return $this->model->tags()->get()->toArray();
})
->form(function () {
$type = Str::singular($this->model->getTable());

return [
MultiSelect::make('tags')
->relationship('tags', 'name')
->createOptionForm(
[
TextInput::make('name'),
TextInput::make('description'),
]
)
->createOptionUsing(function(array $data) use ($type) {
$tag = Tag::create([
'name' => $data['name'],
'description' => $data['description'],
'category' => $type
]);

return $tag->id;
})
->searchable()
->options(
function () {
return Tag::query()->pluck('name', 'id');
}
)
->preload(),
];
})
It doesn't return an error. It just doesn't fill the MultiSelect field (It works when not using livewire component). If I try to create a new register, it returns Call to a member function isRelation() on null I imagine that is something about relationships, could anyone help me please ? I'm losing hairs rsrsrs In resume. I need to use relationship in Action using Livewire component. up
Solution
Dennis Koch
Dennis Koch5mo ago
You need to provide your model to the action. Probably ->record(fn ($livewire) => $livewire->model) or similar
Dennis Koch
Dennis Koch5mo ago
For filling:
->fillForm(function () {
return ['tags' => $this->model->tags()->pluck('id')];
})
->fillForm(function () {
return ['tags' => $this->model->tags()->pluck('id')];
})
leoblanski
leoblanski5mo ago
♥️ ♥️ ♥️ ♥️ ♥️ ♥️ ♥️ ♥️ ♥️ ♥️ ♥️ ♥️ It works guy !!!! Really thank you !!!!
->record($this->taggable)
->record($this->taggable)