Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
Laravel livewire display session message with javascript

I am trying to submit a contact form using laravel livewire like:

public $name;
public $email;
public $phone;
public $message;

public function submitForm()
{

    $attributes['name'] = $this->name;
    $attributes['email'] = $this->email;
    $attributes['phone'] = $this->phone;
    $attributes['message'] = $this->message;

    \App\Models\ContactForm::create($attributes);

    $this->resetForm();

    session()->flash('success', 'Your message has been sent successfully!');


    $this->emit('formSubmitted');

}

Now I want to display the success message using JS on my front-end so I am doing it like this:

<script type="text/javascript">
        $( document ).on("DOMContentLoaded", () => {
            Livewire.on('formSubmitted', message => {

                console.log('{{ session('success') }}');

            });

        });
    </script>

The problem is that in console I am getting "empty string" instead of the session message, but when I write some static data in console.log() it gets printed in console, means that the event is being fired from the component and the response is being triggered but the session data is not being passed in the script

Also when I print the session message in blade like:

{{ session('success') }}

it gets printed properly. Can someone help me out?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

there is no need to save the message in the session's flash, you can do as follow, dispatch a browser event from the livewire component with the message and listen to it in the front-end

$this->dispatchBrowserEvent('formSubmitted',['success' => 'Your message has been sent successfully!']);

and then listen to it in javascript

$( document ).on("DOMContentLoaded", () => {
      // message is argument which is passed to closure, normally we pass as 
      // event
     Livewire.on('formSubmitted', message => {
         console.log(message.detail.success);
     });
});

for a detailed view visit Livewire official Dispatching Browser Events

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda