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

ctrlaltdelme's avatar

Dispatching an event to another component and rendering something inside/in place of that component?

Hopefully the title makes sense. If it doesn't, hopefully my explanation will be better.

I'm trying to work on the users' settings page where they can edit their profile and select some favorite movies from the db. I have it sort of working at the moment but not without a lot of struggling and am not sure if I'm doing it the best way and it still isn't fully correct (see screenshot lol).

I have the following Views and Components.

favorite-movie-card.blade.php

<div class="p-10 bg-zinc-800 rounded-sm shadow-lg border border-zinc-700 cursor-pointer group flex
        justify-center items-center min-h-40"
     x-data="{ showModal: false }"
     @click="showModal = true">

    @if(!$selectedMovie)
        <x-icon-add class="w-8 h-8 stroke-zinc-700 stroke-2
                drop-shadow-md
                group-hover:stroke-zinc-300
                transition-all ease-in-out
                cursor-pointer"/>
    @else
        <img src="{{ $selectedMovie['poster_path'] }}"
             alt="{{ $selectedMovie['title'] }}"
             class="w-full h-full object-cover">
    @endif

    <template x-teleport="body">
        <livewire:favorite-movie-modal x-show="showModal" @close="showModal = false"/>
    </template>
</div>

FavoriteMovieCard.php

<?php

namespace App\Livewire;

use App\Models\Movie;
use Livewire\Attributes\On;
use Livewire\Component;

class FavoriteMovieCard extends Component
{
    public int $movieId;
    public Movie $selectedMovie;

    #[On('movie-selected')]
    public function setMovie(int $movieId): void
    {
        $this->movieId = $movieId;
        $movie = Movie::findOrFail($movieId);

        $this->selectedMovie = $movie;
    }

    public function render()
    {
        return view('livewire.favorite-movie-card');
    }
}

favorite-movie-search.blade.php

FavoriteMovieSearch.php

For brevity, the favorite-movie-search is inside the favorite-movie-modal (not shown). I'm trying to dispatch an event from the Search to the Card so that the Card is replaced with the poster image from the movie. It kind of works, but...https://i.imgur.com/UgSUw1h.png.

I want it to just occupy the card slot that was clicked on, but am not sure how to do that. And one other obstacle is binding all of these to some sort of array of favorite movies that will be passed into my form. They aren't input elements so I can't just use $request->... to get the value.

Lastly, I'm passing the movie ID here which I think is best, but I'm passing the entire movie in some places too I believe. If I'm not doing it here, I'm doing it in a lot of other places and idk what is best; to pass the entire movie, regardless of what its passing to needs the entire Movie and its data, or to pass the ID and make the SQL query for the needed data in the passed-to component/View.

Any help here is appreciated!

0 likes
1 reply
ctrlaltdelme's avatar

Any help is appreciated. If I need to clarify more what I'm trying to do, let me know. I can't always put what I'm thinking into words :\

Please or to participate in this conversation.