Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Wakanda's avatar
Level 10

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.
    }

0 likes
10 replies
automica's avatar

if you are calling this method from your livewire component, you should have all the data you need without needing to reload the page

can you show your livewire component to give some context on how your updateCurrency() method is being used?

Wakanda's avatar
Level 10

@automica my view component

<div class="up-item mr-2">
			<select wire:model="currency" style="border: none !important;">
				@foreach($currencies as $currency)
				<option value="{{ $currency->id }}">{{ $currency->name }}</option> 
				@endforeach
			</select>
</div>

component class

<?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'));
    }
}

Wakanda's avatar
Level 10

I need to do a whole page fresh to refresh some parts of the page that are not part of the livewire component

automica's avatar

@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.

Wakanda's avatar
Level 10

its not a form its via wire:model

<div class="up-item mr-2">
			<select wire:model="currency" style="border: none !important;">
				@foreach($currencies as $currency)
				<option value="{{ $currency->id }}">{{ $currency->name }}</option> 
				@endforeach
			</select>
</div>
Wakanda's avatar
Level 10

@automica the reason why am using wire:model is because I don't want a user to press a button after selecting a currency of their choice

Snapey's avatar

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.

1 like
Wakanda's avatar
Level 10

@snapey I want to go achieve the second option you mentioned. An example code will be much appreciated

Snapey's avatar
Snapey
Best Answer
Level 122

In the view that includes the currency component

    @livewire('nav-cart',['page'=>request()->fullUrl()])

Then in the component, mount method

    public function mount($page)
    {

        $this->page = $page;

create a public property $page, and then after changing the currency

    return redirect($this->page);
2 likes

Please or to participate in this conversation.