1

In a view I include a livewire component like so:

<livewire:search-comments :post="$post">

I can pass in the post as shown above. But the problem is that this is only done on mount():

public function mount($post)
{
    $this->post = $post;
}

In render(), I have:

$comments = $this->post->comments()
    ->where('comment', 'like', "%{$this->search}%")
    ->get()

The problem is that I need to access $post on every render() call, because the comments searched are tied to the post the user is currently on. I could pass the post ID in a hidden field but that doesn't feel like the right (and a safe..?) solution.

4

1 回答 1

1

there are various way to achieve this. Like you can emit an event when user tries to load a new post. then use magic method $refresh to refresh the component.

  protected $listeners = [
        'newPostLoad' => '$refresh',
    ];

or,

you can pass post id while use try to load new post. like this:

// as per your snippets above I assume ur blade has post collection which holds id for each posts.


wire:click=loadNewPost(id) 

then u can reset your $this->post inside loadNewPost function.

Hope this helps. let me know. I will elaborate again

于 2020-03-13T07:09:48.893 回答