在视图中,我包括一个livewire组件,如下所示:
<livewire:search-comments :post="$post">
我可以通过上面显示的帖子。但是问题是,这仅在mount()
以下位置完成:
public function mount($post)
{
$this->post = $post;
}
在中render()
,我有:
$comments = $this->post->comments()
->where('comment', 'like', "%{$this->search}%")
->get()
问题是我需要$post
在每次render()
通话时都进行访问,因为搜索到的评论与用户当前所在的帖子有关。我可以在一个隐藏的字段中传递帖子ID,但这听起来不像是正确的(也是安全的..?)解决方案。
有多种方法可以实现这一目标。就像您可以在用户尝试加载新帖子时发出事件。然后使用魔术方法$ refresh刷新组件。
protected $listeners = [
'newPostLoad' => '$refresh',
];
要么,
您可以在尝试加载新帖子时传递帖子ID。像这样:
// as per your snippets above I assume ur blade has post collection which holds id for each posts.
wire:click=loadNewPost(id)
然后您可以在loadNewPost函数中重置$ this-> post。
希望这可以帮助。让我知道。我会再详细说明
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句