May 3, 2024
0
Level 1
How to pass laravel echo event to livewire component?
Hi Everyone! I am trying to implement pusher and echo in project. I am still learning and I checked the documentation and forum articles as well. I am not able to figure out what's wrong with the code.
I am trying to render listing on real time in admin panel whenever I create a new form from user end.
Here is my code:
class FormSubmitted implements ShouldBroadcastNow { use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*/
public $formData;
public function __construct($formData)
{
$this->formData = $formData;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new Channel('forms'),
];
}
}
I blade file I have created a livewire component and passing $form for listing.
<tbody >
@forelse ($forms as $key=>$form)
<livewire:form-table-body :forms="$form" />
@empty
@endforelse
</tbody>
This is my script added under blade file
<script type="module">
Echo.channel('forms')
.listen('FormSubmitted', (event) => {
Livewire.emit('formDataUpdated', event.formData);
});
</script>
This is livewire function
Please or to participate in this conversation.