I recently followed the tutorial "Global Modals with Livewire and Alpine.js" (https://codecourse.com/watch/global-modals-with-livewire) to add a modal to my project. Everything is working great, but I am now trying to determine how I can pass a parameter to the modal so I can display info from the database in the modal.
I have a page with a table listing football players. When the player name is clicked a modal is triggered using the method explained in the tutorial.
<button x-data="{}" x-on:click="window.livewire.emitTo('components.player-modal', 'showModal')">{{ $player->first_name . ' ' . $player->last_name }}</button>
I want to pass that "$player" record to the modal and haven't been able to find anything online to assist. Would I use the button's x-on:click to pass the data?
Assistance greatly appreciated!!
Hello @alexjolley,
I am assuming showModal
is a Livewire method here. I am also assuming you have a Player
model.
You can pass it directly to the showModal
method. Remember to accept it in the method too.
window.livewire.emitTo('components.player-modal', 'showModal', '{{ $player->id }}')
public function showModal(Player $player)
{
dd($player);
}
@alexjolley You are welcome! Don't forget to mark the best answer so people know it is solved. :)