-
Notifications
You must be signed in to change notification settings - Fork 224
/
query-string.blade.php
executable file
·106 lines (78 loc) · 3.02 KB
/
query-string.blade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
* [Introduction](#introduction)
* [Keeping A Clean Query String](#clean-query-string)
* [Query String Aliases](#query-string-aliases)
## Introduction {#introduction}
Sometimes it's useful to update the browser's query string when your component state changes.
For example, if you were building a "search posts" component, and wanted the query string to reflect the current search value like so:
`https://your-app.com/search-posts?search=some-search-string`
This way, when a user hits the back button, or bookmarks the page, you can get the initial state out of the query string, rather than resetting the component every time.
In these cases, you can add a property's name to `protected $queryString`, and Livewire will update the query string every time the property value changes, and also update the property when the query string changes.
@component('components.code-component')
@slot('class')
class SearchPosts extends Component
{
public $search;
protected $queryString = ['search'];
public function render()
{
return view('livewire.search-posts', [
'posts' => Post::where('title', 'like', '%'.$this->search.'%')->get(),
]);
}
}
@endslot
@slot('view')
@verbatim
<div>
<input wire:model="search" type="search" placeholder="Search posts by title...">
<h1>Search Results:</h1>
<ul>
@foreach($posts as $post)
<li>{{ $post->title }}</li>
@endforeach
</ul>
</div>
@endverbatim
@endslot
@endcomponent
### Keeping A Clean Query String {#clean-query-string}
In the case above, when the search property is empty, the query string will look like this:
`?search=`
There are other cases where you might want to only represent a value in the query string if it is NOT the default setting.
For example, if you have a `$page` property to track pagination in a component, you may want to remove the `page` property from the query string when the user is on the first page.
In cases like these, you can use the following syntax:
@component('components.code-component')
@slot('class')
class SearchPosts extends Component
{
public $foo;
public $search = '';
public $page = 1;
protected $queryString = [
'foo',
'search' => ['except' => ''],
'page' => ['except' => 1],
];
...
}
@endslot
@endcomponent
### Query String Aliases {#query-string-aliases}
Additionally, if you want to modify how properties are represented in the URL, Livewire offers a simple syntax for aliasing query strings.
For example, if you want to shorten the URL, where the page property is represented as `p` and search as `s`, you can use the `as` modifier to achieve that outcome.
@component('components.code-component')
@slot('class')
class SearchPosts extends Component
{
public $search = '';
public $page = 1;
protected $queryString = [
'search' => ['except' => '', 'as' => 's'],
'page' => ['except' => 1, 'as' => 'p'],
];
...
}
@endslot
@endcomponent
Now the URL can look like this:
`?s=Livewire%20is%20awesome&p=2`