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

SunnyBoy's avatar

Livewire with SweetAlert2

Hi, Has anyone got this working? I want to use Livewire but hate the fact that the forum seems not open for ALL. Or may be i could't find a way to post my issue at least... anyways coming back to the topic. I am using https://github.com/realrashid/sweet-alert package in my project now after implementing Livewire its a shame that i can't use it as easily as i though. or is there something that i am missing??? Please advice!

0 likes
23 replies
Snapey's avatar

do you mean Livewire forum ? You have problems posting a new topic?

SunnyBoy's avatar

Yes, not sure where to even start... first day i started and tried to help someone share a link from Livewire docs itself and i got the msg "Account on Hold"... LOL 😂😂😂

After browsing for sometime all i could get to Messages -> New Message where it says Add User (And how would I know who to write, anyways msged Caleb but no response).

So pls let me know if there is some secret door??? Hahaha Also let the community know that there might be ppl like trying the same!!!!

THANKS!!!

ApexLeo's avatar

@sunnyboy hello brother. i have not personaly use livewire but i watched this video which show how to submit form.

may be that prove helpfull to u Laravel Livewire

@sunnyboy u can use the same logic to display sweet alert

Snapey's avatar
Snapey
Best Answer
Level 122

I've not experienced any issues accessing the Livewire forum. Sorry can't really suggest anything.

To answer your question, not sure if this is the best way but works for me.

After installing Sweetalert in your view, add an event listener;

        window.addEventListener('swal',function(e){
            Swal.fire(e.detail);
        });

The listener is looking for 'swal' events and within the event, passed the parameters to use with swal.

Then in Livewire component, after for instance, saving a record, fire the event;

$this->dispatchBrowserEvent('swal', ['title' => 'hello from Livewire']);

Note that this is not using any element of realrashid wrapper.

More parameters can be passed according to the sweetalert2 docs.

eg

$this->dispatchBrowserEvent('swal', [
	'title' => 'Feedback Saved',
	'timer'=>3000,
	'icon'=>'success',
	'toast'=>true,
	'position'=>'top-right'
]);
8 likes
zerosAndOnes's avatar

Hello @snapey. I am using a livewire component in my blade file (a blog index page, for example). On that page, I have a link to create a new post. When I click on this link, it takes me to a page where I'd create a new post and after this, I redirect back to the index page with sweetalert toast success. Usually, this works, but because I have a livewire component on that page, the sweetalert doesnt work. Anyway you could help suggest a workaround for this? Thanks.

sayedsadat's avatar

it is not working for me.

installed sweetalert

used the codes u posted

in view file

<script>
  window.addEventListener('swal',function(e){
        Swal.fire(e.detail);
    });

</script>

in component class file

$this->dispatchBrowserEvent('swal', [
'title' => 'Feedback Saved',
'timer'=>3000,
'icon'=>'success',
'toast'=>true,
'position'=>'top-right'
]);

in master layout file

@include('sweetalert::alert')

but still not working

LongSu's avatar

@Snapey dispatchBrowserEvent is deprecated. I am not sure how to display confirmation dialog using sweetalert2 and delete record by passing id to component from livewire frontend. I am spending a lot of time with this. No any useful document on google. Weird. I can't find it. Please help me, thank you.

Snapey's avatar

@LongSu from the docs

In Livewire 2, Livewire had two different PHP methods for triggering events:

emit()
dispatchBrowserEvent()

Livewire 3 has unified these two methods into a single method:

dispatch()

So I expect my example to simply change dispatchBrowserEvent to just dispatch with the same parameters

1 like
Mawunyo's avatar

Hi @snapey . Please how can I use your solution to delete data in laravel livewire ?

Snapey's avatar

@mawunyo what has that to do with sweet alert? better start a question.

Snapey's avatar

@binggle but this question is about getting sweetalert working, which is perfectly possible and the article you linked above is incorrect in its assumption.

The technique is exactly the same. Emit a browser event to fire the alert.

1 like
LongSu's avatar

dispatchBrowserEvent is deprecated. I am not sure how to display confirmation dialog using sweetalert2 and delete record by passing id to component from livewire frontend. I am spending a lot of time with this. No any useful document on google. Weird. I can't find it. Please help me, thank you.

Snapey's avatar

@LongSu You have to fire an event to sweetalert as described above, and then when the user confirms using the sweetalert confirmation, catch the event that sweetalert fires and forward it back to Livewire.

Don't overlook the simple option of using Livewires built in confirmation alert. https://livewire.laravel.com/docs/wire-confirm

LongSu's avatar

@Snapey , thank you. Do you know this ?

I am trying to load google map api link in livewire modal component, but I assure that it's just asset link. So, I added it in @assets directive , after that , I created callback function which will be calling from google map api. I am using wire-element/modal package from github. So something like this. This is a modal component blade file.

<div>
    <div>
        <x-input-label for="geoAddress" :value="__('Search Address for Map')" />
        <input wire:model.blur="geoAddress" type="text" id="geoAddress" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full px-4 py-2 mt-2" required />
    </div>
    <div wire:ignore>
        <div id="address-map-container" class="mt-2 w-full h-[100px]">
            <div style="width: 100%; height: 100%" id="map"></div>
        </div>
    </div>
</div>

@assets
<script src="https://maps.googleapis.com/maps/api/js?key={My google map api key}&callback=initAutocomplete&libraries=places&v=weekly" defer></script>
@endassets

@script
<script>
function initAutocomplete() {
	// my code here.
}
</script>
@endscript

But when I open a modal, error is showing with "Uncaught (in promise) InvalidValueError: initAutocomplete is not a function". I know this error why. It's because initAutomcomplete() function will be defined after fully loaded links in @assets. Livewire 3 document mentions that @script directive will be loaded after fully loaded links in @assets.

So, I tried to put google map api link like this. But I know this will not work.

@script
<script>
function initAutocomplete() {
	// my code here.
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key={My google map api key}&callback=initAutocomplete&libraries=places&v=weekly" defer></script>
@endscript

Then, I assume , there is no way to load google map api link. Please help me how to load api link or let me know if there is any google map api package that I can use with livewire 3. Thank you so much!

LongSu's avatar

@Snapey Thank you for your reply. Sweetalert issue was resolved, but I am having an issue related to google map api. Can you please help me? I asked it above in detail. Thank you so much!.

Snapey's avatar

@LongSu No. You should remove this totally different question from this thread.

Please or to participate in this conversation.