Hi I have created livewire component for my project i am setting array of ids getting from database in public property in render method of my component, on wire click method this property showing null no ids of array.
Now i have to call db query again to fetch these ids again, how can i keep these ids set in render method available in click method no need to call db query again. need some help what i am doing wrong, what is the right way todo it.
Public property set in rendor method show null on wire:click method
We’ll need to see some code to help you troubleshoot. Typically, a wire:click
method won’t change anything you don’t want it to and the public property should remain untouched unless it’s being affected by something else in your code.
here is sample of my code. I am setting ids in render folders_ids and assets_ids
class Shared extends Component
{
public $selected_files = [];
public $selected_folders = [];
public $asset_ids = null;
public $folders_ids = null;
public function render()
{
$share = $this->getData();
$this->share = $share;
if ($share->folders) {
$this->folders_ids = $share->folders->pluck('id', 'id')->toArray();
}
if ($share->assets) {
$this->assets_ids = $share->assets->pluck('id', 'id')->toArray();
}
return view('livewire.share.shared', compact('share'));
}
public function mount($slug, $share)
{
$this->slug = $slug;
}
public function selectAll()
{
$this->reset(['selected_files', 'selected_folders']);
if (!$this->selected) {
$share = $this->getData();
if ($share->folders) {
$this->selected_folders = $share->folders->pluck('id', 'id')->toArray();
}
if ($share->assets) {
$this->selected_files = $share->assets->pluck('id', 'id')->toArray();
}
$this->makeHash();
}
$this->selected = 1 - $this->selected;
}
}
Wire click code
<button type="button" class="btn btn-secondary" wire:click="selectAll">
<i class="fas @if ($selected)fa-times @else fa-check-double @endif mr-1"></i>
@if ($selected)Deselect All @else Select All @endif
</button>