Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel livewire, how to pass the id or data to another component by click

I have two components Posts and Post, Posts show the posts and by clicking the image I want to show the data of clicked post in another component.

Posts class and component down below:

Component View:


<div class="post" x-data="{open:false}">
  @foreach($posts as $post)
    <div>
      <h1>{{ $post->name }}</h1>
      <h3>{{ $post->body }}</h3>
      <img @click="open = !open" wire:click="showPost({{ $post->id }})" src="{{ $post->image }}" alt="">
    </div>
  @endforeach


<livewireL:post>

    </div>

Component Class:

class Posts extends Component
{


  public $posts, $post;

  public function mount(){
    $this->posts = Post::all();

  }


  public function showPost($id){
    $post = Post::find($id);
    $this->post = $post;
  }

    public function render()
    {
        return view('livewire.posts');
    }
}

and this is the Post component and class that I want to show the clicked data in this component, I have tried $emit and many as documentation but no result.

Component view which I want to render that data:


<div x-show="open">
  <h1>{{ $post->name }}</h1>
  <h3>{{ $post->body }}</h3>
  <img src="{{ $post->image }}">
</div>

Class which I want to pass data:

class Post extends Component
{

  public $post;



  public function mount($id)
  {
    $this->post = \App\Post::find($id);
  }



    public function render()
    {
        return view('livewire.post');
    }
}
like image 207
Mehdi Yaghoubi Avatar asked Mar 12 '20 09:03

Mehdi Yaghoubi


People also ask

How do I transfer data from one component to another in livewire?

You can pass data into a component by passing additional parameters into the <livewire: tag. For example, let's say we have a show-post component. Here's how you would pass in a $post model. Alternatively, this is how you can pass in parameters using the Blade directive.

How do I refresh one livewire component from another component?

For this, you can use Livewire's events system. First, define the listeners on the Livewire component you want to refresh remotely, so that sending an event called refreshComponent will call Livewire's magic $refresh method. protected $listeners = ['refreshComponent' => '$refresh'];

What does livewire do in laravel?

Laravel Livewire is a library that makes it simple to build modern, reactive, dynamic interfaces using Laravel Blade as your templating language. This is a great stack to choose if you want to build an application that is dynamic and reactive but don't feel comfortable jumping into a full JavaScript framework like Vue.


1 Answers

You have to use events to pass data from one component to another component like below.

Component A Blade:

  <img @click="open = !open" wire:click="showPost({{ $post->id }})" src="{{ $post->image }}" alt="">

Component A Class:

public function showPost($id){
    $post = Post::find($id);
    $this->post = $post;
    $this->emit('newPost', $post->id);
  }

you can now catch that event from other livewire component like this:

Component B Class:

class Post extends Component
{

  public $post;

  protected $listeners = ['newPost'];

  public function mount($id)
  {
    $this->post = \App\Post::find($id);
  }

   public function render()
   {
        return view('livewire.post');
   }

   public function newPost($postId)
   {
       // here u have the id in your other component. 
   }
}

you can achieve this other way also. You can pass the id from your component blade as well check this out.

like image 115
fahim152 Avatar answered Sep 17 '22 18:09

fahim152