SKILL.blade.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ---
  2. name: livewire-development
  3. description: "Use for any task or question involving Livewire. Activate if user mentions Livewire, wire: directives, or Livewire-specific concepts like wire:model, wire:click, invoke this skill. Covers building new components, debugging reactivity issues, real-time form validation, loading states, migrating from Livewire 1 to 2, and performance optimization. Do not use for non-Livewire reactive UI (React, Vue, Alpine-only, Inertia.js) or standard Laravel forms without Livewire."
  4. license: MIT
  5. metadata:
  6. author: laravel
  7. ---
  8. @php
  9. /** @var \Laravel\Boost\Install\GuidelineAssist $assist */
  10. @endphp
  11. # Livewire Development
  12. ## Documentation
  13. Use `search-docs` for detailed Livewire 2 patterns and documentation.
  14. ## Basic Usage
  15. ### Creating Components
  16. Use the `{{ $assist->artisanCommand('make:livewire [Posts\\CreatePost]') }}` Artisan command to create new components.
  17. ### Fundamental Concepts
  18. - State should live on the server, with the UI reflecting it.
  19. - All Livewire requests hit the Laravel backend; they're like regular HTTP requests. Always validate form data and run authorization checks in Livewire actions.
  20. ## Livewire 2 Specifics
  21. - `wire:model` is live by default (real-time updates without modifier).
  22. - Components typically exist in the `App\Http\Livewire` namespace.
  23. - Use `emit()`, `emitTo()`, `emitSelf()`, and `dispatchBrowserEvent()` for events.
  24. - Alpine is included separately from Livewire.
  25. ## Best Practices
  26. ### Component Structure
  27. - Livewire components require a single root element.
  28. - Use `wire:loading` and `wire:dirty` for delightful loading states.
  29. ### Using Keys in Loops
  30. @boostsnippet("Wire Key in Loops", "blade")
  31. @foreach ($items as $item)
  32. <div wire:key="item-{{ $item->id }}">
  33. {{ $item->name }}
  34. </div>
  35. @endforeach
  36. @endboostsnippet
  37. ### Lifecycle Hooks
  38. Prefer lifecycle hooks like `mount()`, `updatedFoo()` for initialization and reactive side effects:
  39. @boostsnippet("Lifecycle Hook Examples", "php")
  40. public function mount(User $user) { $this->user = $user; }
  41. public function updatedSearch() { $this->resetPage(); }
  42. @endboostsnippet
  43. ## JavaScript Hooks
  44. You can listen for `livewire:load` to hook into Livewire initialization:
  45. @boostsnippet("Livewire Load Hook Example", "js")
  46. document.addEventListener('livewire:load', function () {
  47. Livewire.onPageExpired(() => {
  48. alert('Your session expired');
  49. });
  50. Livewire.onError(status => console.error(status));
  51. });
  52. @endboostsnippet
  53. ## Testing
  54. @boostsnippet("Example Livewire Component Test", "php")
  55. Livewire::test(Counter::class)
  56. ->assertSet('count', 0)
  57. ->call('increment')
  58. ->assertSet('count', 1)
  59. ->assertSee(1)
  60. ->assertStatus(200);
  61. @endboostsnippet
  62. @boostsnippet("Testing Livewire Component Exists on Page", "php")
  63. $this->get('/posts/create')
  64. ->assertSeeLivewire(CreatePost::class);
  65. @endboostsnippet
  66. ## Common Pitfalls
  67. - Forgetting `wire:key` in loops causes unexpected behavior when items change
  68. - Not validating/authorizing in Livewire actions (treat them like HTTP requests)
  69. - Forgetting that `wire:model` is live by default in v2 (may cause performance issues)