How can I refresh the whole page after livewire ajax
Hi Devs,
How can I refresh the whole page after livewire ajax
my class code
public function updatedCurrency()
{
$currency = Currency::where('id', $this->currency)->first();
session()->put('currency', [
'id' => $currency->id,
'name' => $currency->name,
'code' => $currency->code,
'symbol' => $currency->symbol,
'exchange_rate' => $currency->exchange_rate,
]);
session()->flash('success', "Currency changed to $currency->name");
return redirect(route('welcome')); // currently redirecting to another page and I want to refresh the current page after this logic.
}
<?php
namespace App\Http\Livewire;
use App\Currency;
use Livewire\Component;
use Gloudemans\Shoppingcart\Facades\Cart;
class NavCart extends Component
{
public $currency;
public $cartCount;
public $wishlistCount;
protected $listeners = ['productAdded' => 'incrementCartCount', 'wishlistAdded' => 'incrementWishlistCount'];
// when a user clicks add to cart refresh to get the new cart count
public function incrementCartCount()
{
$this->cartCount = Cart::instance('default')->count();
}
// on mount get the cart items count
public function mount()
{
if (!empty(session('currency'))) {
$this->currency = session('currency')['id'];
}
$this->cartCount = Cart::instance('default')->count();
$this->wishlistCount = Cart::instance('wishlist')->count();
}
// when a user clicks add to cart refresh to get the new wishlist count
public function incrementWishlistCount()
{
$this->wishlistCount = Cart::instance('wishlist')->count();
}
// switch currently
public function updatedCurrency()
{
$currency = Currency::where('id', $this->currency)->first();
session()->put('currency', [
'id' => $currency->id,
'name' => $currency->name,
'code' => $currency->code,
'symbol' => $currency->symbol,
'exchange_rate' => $currency->exchange_rate,
]);
session()->flash('success', "Currency changed to $currency->name");
return redirect(route('welcome'));
}
public function render()
{
$currencies = Currency::orderBy('created_at', 'DESC')->get();
return view('livewire.nav-cart', compact('currencies'));
}
}
@loyd you don't want a redirect in a livewire component.
If this updatedCurrency() is being called via a form action, then it shouldn't be in the livewire component. Updating Currency shouldn't be the responsibility of your NavCart - it belongs in your Currency Controller.
Then you can redirect back
return redirect()->back();
and that will take you back to the page you sent the update from.
If you want other parts of the page to change automatically then THEY must also be livewire components, then you can emit an event that they listen to and re-render when they hear the currency has changed.
If you don't want them to change to Livewire, you need to redirect to the same page from Livewire. To know what route to reload, you may need to pass it into the currency component as a mount property.