I capitalized Volt, because I've been looking for a solution to this problem for months now, and every single result offers a livewire solution, which does not work with Volt components. As shown below.
I have two volt components on the same page, one for listing products, one for creating a new one. When I create a new product, I want listing component to refresh and show the newly added one. Any idea how I can do that?
store_listings.php
<?php
use App\Models\Store;
use Livewire\Volt\Component;
new class extends Component {
public Store $store;
public function deleteListing($id)
{
$this->store->listings()->find($id)->delete();
}
};
create_listing.php
<?php
use App\Models\Store;
use Livewire\Volt\Component;
new class extends Component {
public Store $store;
public $title;
public $price;
public $valid_until;
public function mount()
{
$this->valid_until = now()->addDay()->format('Y-m-d');
}
public function createListing()
{
$this->store->listings()->create([
'title' => $this->pull('title'),
'price' => $this->pull('price') * 100,
'valid_until' => $this->pull('valid_until'),
'sold' => false
]);
}
};
I found this answer while googling. I did everything they said, but after I create a new listing it shows a black and empty modal on the page like I ran dump().
Here's how I tried it :
store_listings.php
use Livewire\Attributes\On;
...
#[On('refresh-the-component')]
public function refresh()
{
$this->refresh();
}
...
create_listing.php
public function createListing()
{
$this->store->listings()->create([
...
]);
$this->dispatch('refresh-the-component');
}