class CollectionEdit extends Component implements HasForms, HasTable
{
use InteractsWithTable;
use InteractsWithForms;
public ?array $data = [];
public array $selectedTableRecords = [];
public Collection $collection;
public function mount(Collection $collection = null): void
{
$this->collection = $collection ?? new Collection();
$this->selectedTableRecords = $this->collection->projects()->pluck('id')->map(function ($id) {
return (string)$id;
})->toArray();
$this->form->fill($this->collection->toArray());
}
public function render()
{
return view('livewire.collection-edit');
}
public function table(Table $table): Table
{
return $table
->query(Project::query())
->columns([
TextColumn::make('title')
->label('Titre')
])
->bulkActions([
BulkAction::make('update_collection')
->label('Mettre à jour la collection')
->icon('heroicon-o-arrow-path')
->action(function (\Illuminate\Support\Collection $records) {
$this->selectedTableRecords = $records->pluck('id')->toArray();
$this->collection->projects()->sync($this->selectedTableRecords);
$this->dispatch('collection-updated');
})
->requiresConfirmation()
])
->defaultPaginationPageOption(25);
}
}