forked from livewire/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.blade.php
executable file
·301 lines (259 loc) · 16.2 KB
/
reference.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
Already familiar with Livewire and want to skip the long-form documentation? Here's a giant list of everything available in Livewire.
### Template Directives
These are directives added to elements within Livewire component templates.
@component('components.code', ['lang' => 'html'])
<button wire:click="save">...</button>
@endcomponent
@component('components.table')
Directive | Description
--- | ---
`wire:key="foo"` | Acts as a reference point for Livewire's DOM diffing system. Useful for adding/removing elements, and keeping track of lists.
`wire:click="foo"` | Listens for a "click" event, and fires the "foo" method in the component.
`wire:click.prefetch="foo"` | Listens for a "mouseenter" event, and "prefetches" the result of the "foo" method in the component. Then, if it is clicked, will swap in the "prefetched" result (without an extra request), if it's not clicked, will throw away the cached result.
`wire:keydown.enter="foo"` | Listens for a keydown event on the `enter` key, which fires the "foo" method in the component.
`wire:foo="bar"` | Listens for a browser event called "foo". (You can listen for *any* browser DOM event - not just those fired by Livewire).
`wire:model="foo"` | Assuming `$foo` is a public property on the component class, every time an input element with this directive is updated, the property synchronizes with its value.
`wire:model.debounce.100ms="foo"` | Debounces the `input` events emitted by the element every 100 milliseconds.
`wire:model.lazy="foo"` | Lazily syncs the input with its corresponding component property at rest.
`wire:model.defer="foo"` | Deferrs syncing the input with the Livewire property until an "action" is performed. This saves drastically on server roundtrips.
`wire:poll.500ms="foo"` | Runs the "foo" method on the component class every 500 milliseconds.
`wire:init="foo"` | Runs the "foo" method on the component immediately after it renders on the page.
`wire:loading` | Hides the element by default, and makes it visible while network requests are in transit.
`wire:loading.class="foo"` | Adds the `foo` class to the element while network requests are in transit.
`wire:loading.class.remove="foo"` | Removes the `foo` class while network requests are in transit.
`wire:loading.attr="disabled"` | Adds the `disabled="true"` attribute while network requests are in transit.
`wire:dirty` | Hides the element by default, and makes it visible while the element's state is "dirty" (different from what exists on the backend).
`wire:dirty.class="foo"` | Adds the `foo` class to the element while it's dirty.
`wire:dirty.class.remove="foo"` | Removes the `foo` class while the element is dirty.
`wire:dirty.attr="disabled"` | Adds the `disabled="true"` attribute while the element's dirty.
`wire:target="foo"` | Scopes `wire:loading` and `wire:dirty` functionality to a specific action.
`wire:ignore` | Instructs Livewire to not update the element or its children when updating the DOM from a server request. Useful when using third-party JavaScript libraries within Livewire components.
`wire:ignore.self` | The "self" modifier restricts updates to the element itself, but allows modifications to its children.
@endcomponent
### Alpine Component Object (`$wire`)
These are methods and properties available on the `$wire` object provided to Alpine components within a Livewire template. [Read Full Documentation](/docs/2.x/alpine-js)
@component('components.code', ['lang' => 'html'])
<div x-data>
<span x-show="$wire.showMessage">...</span>
<button x-on:click="$wire.toggleShowMessage()">...</button>
</div>
@endcomponent
@component('components.table')
API | Description
--- | ---
`$wire.foo` | Get the value of the "foo" property on the Livewire component
`$wire.foo = 'bar'` | Set the value of the "foo" property on the Livewire component
`$wire.bar(..args)` | Call the "bar" method (with params) on the Livewire component
`let baz = await $wire.bar(..args)` | Call the "bar" method, but wait for the response and set `baz` to it
`$wire.on('foo', (..args) => {})` | Call a function when the "foo" event is emitted
`$wire.emit('foo', ...args)` | Emit the "foo" event to all Livewire components
`$wire.emitUp('foo', ...args)` | Emit the "foo" event to parent components
`$wire.emitSelf('foo', ...args)` | Emit the "foo" event only to this component
`$wire.get('foo')` | Get the "foo" property
`$wire.set('foo', 'bar')` | Set the "foo" property on the component
`$wire.call('foo', ..args)` | Call the "foo" method with params on the component
`x-data="{ foo: $wire.entangle('foo') }"` | Entagle the value of "foo" between Livewire and Alpine
`$wire.entangle('foo').defer` | Only update Livewire's "foo" next time a Livewire request is fired
@endcomponent
### Global Livewire JavaScript Object
These are methods available on the `window.Livewire` object in the frontend. These are for deeper Livewire interaction and customization.
@component('components.table')
Method | Description
--- | --- | ---
`Livewire.first()` | Get the first Livewire component's JS object on the page
`Livewire.find(componentId)` | Get a Livewire component by it's ID
`Livewire.all()` | Get all the Livewire components on a page
`Livewire.directive(directiveName, (el, directive, component) => {})` | Register a new Livewire directive (`wire:custom-directive`)
`Livewire.hook(hookName, (...) => {})` | Call a method when JS lifecycle hook is fired. [Read more](#js-hooks)
`Livewire.onLoad(() => {})` | Fires when Livewire is first finished loading on a page
`Livewire.onError((message, statusCode) => {})` | Fires when a Livewire request fails. You can `return false` from the callback to prevent Livewire's default behavior
`Livewire.emit(eventName, ...params)` | Emit an event to all Livewire components listening on a page
`Livewire.emitTo(componentName, eventName, ...params)` | Emit an event to specific component name
`Livewire.on(eventName, (...params) => {})` | Listen for an event to be emitted from a component
`Livewire.start()` | Boot Livewire on the page (done for you automatically via `@@livewireScripts`)
`Livewire.stop()` | Tear down Livewire from the page
`Livewire.restart()` | Stop, then start Livewire on the page
`Livewire.rescan()` | Re-scan the DOM for newly added Livewire components
@endcomponent
### JavaScript Hooks {#js-hooks}
These are "hooks" you can listen for in JavaScript. These allow you to hook into very specific parts of a Livewire component's JavaScript lifecycle for third-party packages or deep customizations. The abilities unlocked here are immense. A significant of Livewire's core uses these hooks to provide functionality.
@component('components.code', ['lang' => 'javascript'])
Livewire.hook('component.initialized', component => {
//
})
@endcomponent
@component('components.table')
Name | Params | Description
--- | --- | ---
`component.initialized` | `(component)` | A new component has been initialized
`element.initialized` | `(el, component)` | A new element has been initialized
`element.updating` | `(fromEl, toEl, component)` | An element is about to be updated after a Livewire request
`element.updated` | `(el, component)` | An element has just been updated from a Livewire request
`element.removed` | `(el, component)` | An element has been removed after a Livewire request
`message.sent` | `(message, component)` | A new Livewire message was just sent to the server
`message.failed` | `(message, component)` | A Livewire ajax request (message) failed
`message.received` | `(message, component)` | A message has been received (but hasn't affected the DOM)
`message.processed` | `(message, component)` | A message has been fully received and implemented (DOM updates, etc...)
@endcomponent
### Component Class Lifecycle Hooks
These are methods you can declare in your Livewire component classes to run code at specific times in the backend's lifecycle. [Read Full Documentation](/docs/2.x/lifecycle-hooks)
@component('components.code', ['lang' => 'php'])
class ShowPost extends Component
{
public function mount()
{
//
}
}
@endcomponent
@component('components.table')
Name | Description
--- | ---
`hydrate()` | Called on subsequent Livewire requests after the component has been hydrated, but before any other action occurs
`mount(...$params)` | Called when a Livewire component is newed up (think of it like a constructor)
`updated($field, $newValue)` | Called after a property has been updated
`updatedFoo($newValue)` | Called after the "foo" property has been updated
`updatedFooBar($newValue)` | Called after the nested "bar" key on the "foo" property has been updated
`render()` | Called before "dehydrate" and renders the Blade view for the component
`dehydrate()` | Called after `render()`, but before the component has been dehydrated and sent to the frontend
@endcomponent
### Component Class Protected Properties
Livewire provides core functionality through protected properties on a component's class. Most of these have corresponsing methods by the same name if you prefer to return values in a method, rather than declare them as properties.
@component('components.code', ['lang' => 'php'])
class ShowPost extends Component
{
protected $rules = ['foo' => 'required|min:6'];
}
@endcomponent
@component('components.table')
Name | Description
--- | ---
`$queryString` | Declare which properties to "bind" to the query sting. [Read Docs](/docs/2.x/query-string)
`$rules` | Specify validation rules to be applied to properties when calling `$this->validate()`. [Read Docs](/docs/2.x/input-validation)
`$listeners` | Specify which events you want to listen for emitted by other components. [Read Docs](/docs/2.x/events)
`$paginationTheme` | Specify whether you want to use Tailwind or Bootstrap for you pagination theme. [Read Docs](/docs/2.x/pagination)
@endcomponent
### Component Class Traits
These are traits that unlock additional functionality in a Livewire component. Usually for features that are considered best as "opt-in".
@component('components.code', ['lang' => 'php'])
class ShowPost extends Component
{
use WithPagination;
}
@endcomponent
@component('components.table')
Name | Description
--- | ---
`WithPagination` | This trait enables Livewire-based pagination instead of Laravel's stock pagination system. [Read Docs](/docs/2.x/pagination)
`WithFileUploads` | This trait enables adding `wire:model` to an input of `type="file"`. [Read Docs](/docs/2.x/file-uploads)
@endcomponent
### Class Methods
@component('components.code', ['lang' => 'php'])
class PostForm extends Component
{
public function save()
{
...
$this->emit('post-saved');
}
}
@endcomponent
@component('components.table')
Name | Description
--- | ---
`$this->emit($eventName, ...$params)` | Emit an event to other components on the page
`$this->emit($eventName, ...$params)->up()` | Emit an event to parent components on the page
`$this->emit($eventName, ...$params)->self()` | Emit an event only to THIS component
`$this->emit($eventName, ...$params)->to($componentName)` | Emit an event to any component matching the provided name
`$this->dispatchBrowserEvent($eventName, ...$params)` | Dispatch a browser event from this component's root element
`$this->validate()` | Run the validation rules provided in the `$rules` property against the public component properties
`$this->validate($rules, $messages)` | Run the provided validation rules against the public properties
`$this->validateOnly($propertyName)` | Run the `$rules` property validation against a specific property provided and not others
`$this->validateOnly($propertyName, $rules, $messages)` | Run the provided validation rules against a specific property name
`$this->redirect($url)` | Redirect to a new URL when the Livewire request finishes and reaches the frontend
`$this->redirectRoute($routeName)` | Redirect to a specific route name
`$this->skipRender()` | Skip running the `->render()` method for the current request. (Usually for performance reasons)
`$this->addError($name, $error)` | Add a specific error name and value to the component's error bag manually
`$this->resetValidation()` | Reset the currently stored validation errors (clear them)
`$this->fill([...$propertyData])` | Set public property names to provided values in bulk
`$this->reset()` | Reset all public properties to their initial (pre-mount) state
`$this->reset($field)` | Reset a specific public property to its pre-mount state
`$this->reset([...$fields])` | Reset multiple specific properties
`$this->only([...$propertyNames])` | Return key->value pairs of property data only for a specific set of property names
@endcomponent
### PHP Testing Methods
These are methods available on Livewire's testing helpers. [Read Full Documentation](/docs/2.x/testing)
@component('components.code', ['lang' => 'php'])
public function test()
{
Livewire::test(ShowPost::class)
->assertDontSee('bar')
->set('foo', 'bar')
->assertSee('bar');
}
@endcomponent
@component('components.table')
Name |
--- |
`->assertSet($propertyName, $value)` |
`->assertNotSet($propertyName, $value)` |
`->assertPayloadSet($propertyName, $value)` |
`->assertPayloadNotSet($propertyName, $value)` |
`->assertSee($string)` |
`->assertDontSee($string)` |
`->assertSeeHtml($string)` |
`->assertDontSeeHtml($string)` |
`->assertSeeHtmlInOrder([$firstString, $secondString])` |
`->assertSeeInOrder([$firstString, $secondString])` |
`->assertEmitted($eventName)` |
`->assertNotEmitted($eventName)` |
`->assertDispatchedBrowserEvent($eventName)` |
`->assertHasErrors($propertyName)` |
`->assertHasErrors($propertyName, ['required', 'min:6'])` |
`->assertHasNoErrors($propertyName)` |
`->assertHasNoErrors($propertyName, ['required', 'min:6'])` |
`->assertRedirect()` |
`->assertRedirect($url)` |
`->assertViewHas($viewDataKey)` |
`->assertViewHas($viewDataKey, $expectedValue)` |
`->assertViewHas($viewDataKey, function ($dataValue) {})` |
`->assertViewIs('livewire.some-view-name')` |
@endcomponent
### Artisan Commands
These are the `artisan` commands Livewire makes available to make frequent tasks like creating a component easier.
@component('components.table')
Name | Params | Description
--- | ---
`artisan make:livewire` | Create a new component
`artisan livewire:make` | Create a new component
`artisan livewire:copy` | Copy a component
`artisan livewire:move` | Move a component
`artisan livewire:delete` | Delete a component
`artisan livewire:touch` | Alias for `livewire:make`
`artisan livewire:cp` | Alias for `livewire:copy`
`artisan livewire:mv` | Alias for `livewire:move`
`artisan livewire:rm` | Alias for `livewire:delete`
`artisan livewire:stubs` | Publish Livewire stubs (used in the above commands) for local modification
`artisan livewire:publish` | Publish Livewire's config file to your project (`config/livewire.php`)
`artisan livewire:publish --assets` | Publish Livewire's config file AND its frontend assets to your project
`artisan livewire:configure-s3-upload-cleanup` | Configure your cloud disk driver's S3 bucket to clear temporary uploads after 24 hours
@endcomponent
### PHP Lifecycle Hooks
These are hooks provided by Livewire in PHP for listening to lifecycle occurences at a global level (not at a component level). These are used internally to provide a significant portion of Livewire's core functionality, and can be used in ServiceProviders to further extend Livewire yourself.
@component('components.code', ['lang' => 'php'])
Livewire::listen('component.hydrate', function ($component, $request) {
//
});
@endcomponent
@component('components.table')
Name | Params | Description
--- | ---
`component.hydrate` | `($component, $request)` | Run on EVERY component hydration
`component.hydrate.initial` | `($component, $request)` | Run only on the INITIAL hydration (When the component is first loaded)
`component.hydrate.subsequent` | `($component, $request)` | Run only AFTER the initial component request
`component.dehydrate` | `($component, $response)` | Run on EVERY component dehydration
`component.dehydrate.initial` | `($component, $response)` | Run only on the INITIAL dehydration (When the component is first loaded)
`component.dehydrate.subsequent` | `($component, $response)` | Run on dehydrate AFTER the initial component request
`property.hydrate` | `($name, $value, $component, $request)` | Run when a specific property is hydrated
`property.dehydrate` | `($name, $value, $component, $response)` | Run when a specific property is dehydrated
@endcomponent