About Keeping A Clean Query String

I tried to explain the purpose in words, but it was difficult, so I used images.


To explain it in words
Enter the search text.
Turn the page.
Delete the search text.

Result
Before
Only the search query string will be deleted.
After
Both page query and search query will be deleted.

Component

public $search = ‘’;
public $page = 1;
protected $queryString =
[
‘search’ => [‘except’ => ‘’],
‘page’ => [‘except’ => 1],
];
public function render()
{
$search = $this->search;
$sampledata =Sampledata::where(‘first_name’,‘like’,"%$search%")->latest()->paginate(3);
return view(‘livewire.posts’,compact(‘sampledata’));
}

I hope this is understood.(´∀`)

Not tested, but something like this should will work:

public function hydrate()
{
    $this->page = empty($this->search)
        ? 1
        : $this->page;
}
1 Like

thank you xxdalexx
(^з^)-☆Chu!! (^з^)-☆Chu!! (^з^)-☆Chu!!

\(^o^)/\(^o^)/\(^o^)/\(^o^)/
( ´꒳`)/:heart:︎ヾ(´∀`)ノCongratulations!

I thought your solution was very cool!.
I think this method is great because of its applicability.

I made a few changes to the code after xxdalexx pointed it out.
memorandum:
Component

public $search = ‘’;
public $page = 1;

protected $queryString = [
    'search' => ['except' => ''],
    'page' => ['except' => 1],
];
public function updatedSearch ()
{
    $this->page = empty($this->search) ? 1 : $this->page;
     //The code below was more routine.
     //It would be more natural to reset the code after retyping it.
     //$this->page =  1 ? :1;
}

//there is a problem with pagination.
public function hydrate()
{

}

public function render()
{
    $search = $this->search;
    $sampledata =Sampledata::where('first_name','like',"%$search%")->latest()->paginate(3);
    return view('livewire.posts',compact('sampledata'));
}

blade

<form method="get">
<input class="form-control" type="search" placeholder="Search" aria-label="Search" wire:model="search">
</form>

You’re able to leave out your updatedSearch method, the hydrate method is automatically called at the beginning of every subsequent livewire request after the initial page load.

1 Like

Thank you again \(^o^)/

I did not know about the hydrate method.
While validating, I saw your code and thought it was cool, so I looked at the content and tested it with updateFooBar and it worked.
I took your suggestion and tested it with the hidate method.
Unfortunately, there was a problem with pagination, so I fixed the above code.
Specifically, the “previous” and “next” buttons were malfunctioning.
At the time of posting, I thought it would be rude to fix your code, so I just posted it without verifying it.

Yeah, I forget about what order methods are called in sometimes, and traits as plugins adds to that too. If it’s working correctly in the updatedSearch method, I see no issue putting it there. If you’re still having problems, you’ll have to “reset” the pagination.

if (empty($this->search)) {
    $this->page = 1;
    $this->initializeWithPagination();
}

I don’t remember if the updatedParam methods are called before the trait initialize methods or not.

1 Like

Thank you again and again.
My goal has already been achieved with your wonderful code. Thank you very much.
I have verified the above code and found that there is a problem with the behavior of the previous and next buttons. I will record this as a reminder.
Thank you very much.