To invoke a Livewire method from JavaScript, you can use the Livewire.emit or Livewire.emitTo methods. Here's how you can modify your JavaScript code to call the test method on your Livewire component:
const floor = document.getElementById('page_container');
floor.addEventListener('click', (e) => {
if (e.target.classList.contains('alloc') || e.target.classList.contains('av')) {
if (e.target.id > '') {
// Emit an event to Livewire to invoke the 'test' method
Livewire.emit('test');
// If you need to specify the component, use 'emitTo'
// Replace 'booking-component' with your actual Livewire component name
Livewire.emitTo('booking-component', 'test');
console.log('just ran a seatClick');
}
}
});
And your Livewire component's test method should remain the same:
public function test() {
Log::info('Log message', array('context' => "function test() just run!"));
}
Make sure that your Livewire component's name in the emitTo function matches the actual name of your Livewire component. For example, if your Livewire component is named BookingComponent, you should use Livewire.emitTo('booking-component', 'test');.
Also, ensure that your Livewire scripts are properly loaded on the page where you're trying to invoke the Livewire method from JavaScript. If Livewire's JavaScript is not loaded, the Livewire object will not be available, and you won't be able to emit events to your component.