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

asheek_mohammed's avatar

Dispatching an event when page loads

I am trying to open login modal when page loads if the user is not logged in.

I have put my login component in the layout, so it can dispatched from everywhere.

#[On('login-modal-open')]
    public function openLoginModal($title = 'Welcome Back πŸ‘‹')
    {
        $this->title = $title ;
        $this->open = true;
    }

this is the what listens for the event.

<section class="mb-16 xl:mb-20" x-data="{ 
        initializeLogin() {
            if (!@json(auth()->check())) {
                alert('hello');
                Livewire.dispatchTo('v2.auth.login', 'login-modal-open');
            }
        }
    }" x-init="initializeLogin">
  ....
</section>

this is what i have tried now. This is in page which i am trying to open login component. i have also tried calling from script tag directly. nothing works.

How do i open login component here.

NB: Alert works fine.

Thanks in advance

0 likes
3 replies
Snapey's avatar

Why not fire event from livewire if the user is not logged in?

asheek_mohammed's avatar

@Snapey

I tried dispatching from mount, render and boot method, but it doesnt seem to have any effect.

martinbean's avatar

@asheek_mohammed Why does this need to be an event? Just open the modal on page load:

@guest
    <!-- Some modal mark-up, and code to open modal -->
@endguest

Please or to participate in this conversation.