updating search results, get part of previous result with new result
what happens is, when i click on a category to show related posts, it works initially (fresh page load) - but on subsequent click on a different category, if there are 3 results, 2 of them will be related to NEW cat, the third one will be of the previous category.
i use <a wire:click=method(id)>cat</a> to send cat type to component, re-run the search (which is a lot more convulated than a simple, post::whereType..
the QUERY is actually working.. this is my log statements for initial load, u can see collection id [9] ONE result which is totally FINE
mount
render
search
typeid [2]
comp count [{"Illuminate\\Support\\Collection":[9]}]
then when i click on a diff category you can see 6,7,8
updatType
render
search
typeid [1]
comp count [{"Illuminate\\Support\\Collection":[6,7,8]}]
but on the page i see Ids/models : 9, 7, 8, i am doing a
foreach($posts as $post) {
dump($post->id). // HERE corect IDs will be dumped eg 6, 7, 8
<sub component :post=$post> //inside here if i then do $post->id, i will see 9, 7,8
}
HERE is my component:
<?php
namespace ....
use ..
class Index extends Component
{
#[Url]
public ?int $type = null;
#[Url]
public ?string $search = null;
public $types;
public $posts;
public function mount()
{
info('mount');
$this->types = Type::oldest('name')->get();
}
public function render()
{
info('render');
$this->search();
return view('livewire.posts.index');
}
public function updated()
{
info('updated');
$this->search();
}
public function updateType($id)
{
info('updatType');
$this->type = $id;
}
public function search()
{
info('search');
$this->posts = Post::with('country')
//// query
->get();
info('Post count', [$this->posts->pluck('id')]);
}
}
here is my view:
<div class="index">
<div class="filter">
<p>Please choose what you are looking for</p>
<div class="types">
{{ $this->type }}
@foreach($types as $type)
<div class="type">
<a style="cursor: pointer"
wire:click="updateType({{ $type->id }})"
{{-- href="{{ route('posts.index', ['type' => $type->slug]) }}" --}}
>
<img src="{{ asset($type->image) }}"/>
{{ $type->name }}
</a>
</div>
@endforeach
</div>
</div>
<div class="posts">
@foreach ($posts as $post)
@dump($post->id)
<livewire:posts.card :post="$post"/>
@endforeach
</div>
</div>
here is the post card: (child)
<div class="post">
<img src="{{ asset($post->image) }}"/>
{{ $post->name }}<br/>
{{ $post->id }}<br/>
</div>
Please or to participate in this conversation.