How have you set up your blade view? Have you linked your variables with wire:model?
Jul 31, 2021
8
Level 5
filter isnt reseting , search isnt updating
please kindly help what m i doing wrong , filter isnt reseting , search isnt updating also pagination isnt work
<?php
namespace App\Http\Livewire;
use App\Models\School;
use App\Models\Status;
use App\Models\Post;
use App\Models\Vote;
use Livewire\Component;
use Livewire\WithPagination;
class PostsIndex extends Component
{
use WithPagination;
public $status;
public $school;
public $filter;
public $search;
protected $queryString = [
'status',
'school',
'filter',
'search',
];
protected $listeners = ['queryStringUpdatedStatus'];
public function mount()
{
$this->status = request()->status ?? 'All';
}
public function updatingSchool()
{
$this->resetPage();
}
public function updatingFilter()
{
$this->resetPage();
}
public function updatingSearch()
{
$this->resetPage();
}
public function updatedFilter()
{
if ($this->filter === 'My Exp') {
if (! auth()->check()) {
return redirect()->route('login');
}
}
}
public function queryStringUpdatedStatus($newStatus)
{
$this->resetPage();
$this->status = $newStatus;
}
public function render()
{
$statuses = Status::all()->pluck('id', 'name');
$schools = School::all();
return view('livewire.posts-index', [
'posts' => Post::with('user', 'school', 'status')
->when($this->status && $this->status !== 'All', function ($query) use ($statuses) {
return $query->where('status_id', $statuses->get($this->status));
})->when($this->school && $this->school !== 'All Schools', function ($query) use ($schools) {
return $query->where('school_id', $schools->pluck('id', 'name')->get($this->school));
})->when($this->filter && $this->filter === 'Top Exp', function ($query) {
return $query->orderByDesc('votes_count');
})->when($this->filter && $this->filter === 'My Exp', function ($query) {
return $query->where('user_id', auth()->id());
})->when($this->filter && $this->filter === 'Spam Posts', function ($query) {
return $query->where('spam_reports', '>', 0)->orderByDesc('spam_reports');
})->when(strlen($this->search) >= 3, function ($query) {
return $query->where('body', 'like', '%'.$this->search.'%');
})
->addSelect(['voted_by_user' => Vote::select('id')
->where('user_id', auth()->id())
->whereColumn('post_id', 'posts.id')
])
->withCount('votes')
->withCount('comments')
->orderBy('id', 'desc')
->simplePaginate(10)
->withQueryString(),
'schools' => $schools,
]);
}
}
Please or to participate in this conversation.