Search code examples
laravellaravel-livewire

How to create a child component using laravel 8 livewire?


I'm creating a project using Laravel 8 and Livewire. I have created an admin panel using Livewire which I list in there the clients and their products. Now I want to create a child component connected with the admin component by clicking a button(ex. See Info btn). So that means I have to pass a ID of the client bc every client has different ID. And by clicking the client I want to open a new page which shows the information of the client.

Could you suggest me a tutorial or a way how to do this?


Solution

  • You can pass data into a component by passing additional parameters into the <livewire: tag.

    Step 1: Passing Parameters

    For example, let's say we have a child-client component. Here's how you would pass in a $client_id.

    <livewire:child-client :client_id="$client_id">
    

    Alternatively, this is how you can pass in parameters using the Blade directive.

    @livewire('child-client', ['client_id' => $client_id])
    

    Step 2: Receiving Parameters

    class ShowPost extends Component
    {
        public $client_id;
       
        public function mount($client_id)
        {
            $this->client_id= $client_id;
        }
     
    

    If any doubt, please don't hesitate to ask