### Install Livewire Package with Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/reference Installs the Livewire package into your Laravel project using Composer, making the Livewire framework available for use. ```Bash composer require livewire/livewire ``` -------------------------------- ### Install Livewire with Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/file-downloads This command installs the Livewire package into your Laravel project using Composer, making it available for use. ```bash composer require livewire/livewire ``` -------------------------------- ### Install Livewire Package via Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/installation This command is used to install the Livewire package into your Laravel application using Composer. It fetches the necessary dependencies and integrates Livewire into your project's vendor directory. ```Bash composer require livewire/livewire ``` -------------------------------- ### Install Livewire via Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/turbolinks This command installs the Livewire package into your Laravel project using Composer, making the Livewire backend components and necessary dependencies available for development. ```PHP composer require livewire/livewire ``` -------------------------------- ### Publish Livewire Configuration File Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/installation Provides the Artisan command to publish Livewire's default configuration file, allowing users to customize various settings beyond the default 'zero-configuration' approach. ```php php artisan livewire:publish --config ``` -------------------------------- ### Install Livewire PHP Package Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/quickstart Use Composer to add the Livewire package to your Laravel application, making its functionalities available for use. ```Bash composer require livewire/livewire ``` -------------------------------- ### Automate Livewire Asset Publishing with Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/installation Recommends adding the Livewire asset publishing command to the `post-autoload-dump` scripts in your `composer.json` file. This ensures assets are automatically updated and helps prevent issues during future Livewire updates. ```json { "scripts": { "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover --ansi", "@php artisan vendor:publish --force --tag=livewire:assets --ansi" ] } } ``` -------------------------------- ### Install Livewire with Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/deployment This command installs the Livewire package into your Laravel project using Composer, making it available for use. It's the first step to integrate Livewire into your application. ```PHP composer require livewire/livewire ``` -------------------------------- ### Install Livewire via Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/quickstart Installs the Livewire PHP package into your Laravel project using Composer, adding it as a required dependency. ```Bash composer require livewire/livewire ``` -------------------------------- ### Install Livewire PHP Package with Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/upgrading This command installs the Livewire PHP package into your Laravel project using Composer, making Livewire available for use. ```bash composer require livewire/livewire ``` -------------------------------- ### Publish Livewire Frontend Assets Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/installation Details the Artisan command to publish Livewire's JavaScript assets, enabling them to be served directly by your web server instead of through Laravel. This is useful for specific deployment scenarios. ```php php artisan livewire:publish --assets ``` -------------------------------- ### Install Livewire PHP Package via Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/quickstart This command installs the Livewire PHP package into your Laravel project using Composer, making Livewire's backend functionalities available for use. ```PHP composer require livewire/livewire ``` -------------------------------- ### Configure Livewire Asset Base URL Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/installation Illustrates how to adjust the `asset_url` setting in `config/livewire.php` to resolve issues with asset loading when Livewire assets are published to a sub-folder or the application is hosted on a non-root path. Examples show common configurations. ```php // In config/livewire.php 'asset_url' => '/assets' // or 'asset_url' => '/application' ``` -------------------------------- ### Install Livewire with Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/traits This command installs the Livewire package into your Laravel project using Composer, making it available for use. ```bash composer require livewire/livewire ``` -------------------------------- ### Install Livewire with Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/contribution-guide This command installs the Livewire package into your Laravel project using Composer, making it available for use. ```Composer composer require livewire/livewire ``` -------------------------------- ### Install Livewire Package via Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/index This command installs the Livewire package into your Laravel project using Composer, the PHP dependency manager. It's the first step to integrate Livewire into your application. ```Bash composer require livewire/livewire ``` -------------------------------- ### Example Blade Layout File for Livewire Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/rendering-components Provides a basic example of a traditional Blade layout file, demonstrating the inclusion of `@livewireStyles` and `@livewireScripts` directives, and a `@yield('content')` placeholder for component output. This structure is compatible with Livewire's `@extends` approach. ```Blade @livewireStyles @yield('content') @livewireScripts ``` -------------------------------- ### Install Livewire PHP Package with Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/query-string This command installs the Livewire PHP framework as a dependency in your Laravel project using Composer, making it available for use. ```Shell composer require livewire/livewire ``` -------------------------------- ### Install Livewire PHP Package Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/api This snippet demonstrates how to install the Livewire framework into your Laravel project using Composer, the PHP dependency manager. This command adds Livewire to your project's dependencies. ```PHP composer require livewire/livewire ``` -------------------------------- ### Copying Livewire Components with `copy` Command Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/artisan-commands Illustrates how to duplicate Livewire components using the `php artisan livewire:copy` command. It includes examples for basic copying, forcing an overwrite of an existing component, and copying with the associated test file. ```PHP php artisan livewire:copy foo bar # Copies Foo.php & foo.blade.php to Bar.php and bar.blade.php php artisan livewire:copy foo bar --force # Overwrites existing "bar" component php artisan livewire:copy foo bar --test # Copies Foo.php & foo.blade.php & FooTest.php to Bar.php & bar.blade.php & BarTest.php ``` -------------------------------- ### Livewire Component Blade View (Example Content) Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/file-downloads This example demonstrates adding content to a Livewire component's Blade view. Remember that all content within a Livewire component's view must be wrapped inside a single root HTML element. ```html

Hello World!

``` -------------------------------- ### Livewire Counter Component Updated Blade View Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/reference An updated version of the `Counter` Livewire component's Blade view, demonstrating how to add content within the required single root element. This example adds a simple 'Hello World!' heading. ```Blade

Hello World!

``` -------------------------------- ### Example Blade View for Livewire Component Output Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/rendering-components Provides an example of a Blade view file that would be returned by a Livewire component's `render()` method. It demonstrates iterating over data and including partials, emphasizing the requirement for a single root HTML element. ```Blade
@foreach ($posts as $post) @include('includes.post', $post) @endforeach
``` -------------------------------- ### Example Livewire Dusk Browser Test Structure Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/contribution-guide An example of a Livewire Dusk browser test, demonstrating how to structure a test case to interact with a Livewire component, perform actions like clicks, and assert expected outcomes in the browser. ```PHP /** @test */ public function it_can_run_foo_action { $this->browse(function ($browser) { Livewire::visit($browser, FooComponent::class) /** * Basic action (click). */ ->waitForLivewire()->click('@foo') ->assertSeeIn('@output', 'foo') ; }); } ``` -------------------------------- ### Livewire Component Blade View with Basic Content Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/quickstart An example of adding simple content to the Livewire component's Blade view, demonstrating the single root element requirement. ```Blade

Hello World!

``` -------------------------------- ### Livewire Blade View with Initial Content Source: https://laravel-livewire.com/docs/2.x/quickstart/quickstart Adding basic 'Hello World!' text to the Livewire component's Blade view to make something tangible visible in the browser. ```Blade

Hello World!

``` -------------------------------- ### Example Livewire Component PHP Class Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/deployment This is the generated PHP class for the 'counter' Livewire component. It extends Livewire\Component and defines a render method responsible for returning the Blade view associated with this component. ```PHP namespace App\Http\Livewire; use Livewire\Component; class Counter extends Component { public function render() { return view('livewire.counter'); } } ``` -------------------------------- ### Livewire PHP Examples for Custom File Storage Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/file-uploads Illustrates various methods for storing uploaded files in Livewire, leveraging Laravel's filesystem API. Examples cover storing to default/specific disks, custom filenames, and setting public visibility for uploaded files. ```PHP // Store the uploaded file in the "photos" directory of the default filesystem disk. $this->photo->store('photos'); // Store in the "photos" directory in a configured "s3" bucket. $this->photo->store('photos', 's3'); // Store in the "photos" directory with the filename "avatar.png". $this->photo->storeAs('photos', 'avatar'); // Store in the "photos" directory on S3 with the filename "avatar.png". $this->photo->storeAs('photos', 'avatar', 'S3'); // Store in the "photos" directory, with "public" visibility in a configured "s3" bucket. $this->photo->storePublicly('photos', 's3'); // Store in the "photos" directory, with the name "avatar.png", with "public" visibility in a configured "s3" bucket. $this->photo->storePubliclyAs('photos', 'avatar', 's3'); ``` -------------------------------- ### Create Livewire Component in Sub-folders Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/making-components These commands demonstrate various syntaxes for creating Livewire components within sub-folders. This organizes component files into a directory structure, for example, 'app/Http/Livewire/Post/Show.php' and 'resources/views/livewire/Post/Show.blade.php'. ```php php artisan make:livewire Post\\Show ``` ```php php artisan make:livewire Post/Show ``` ```php php artisan make:livewire post.show ``` -------------------------------- ### Modified Livewire Component Blade View with Content Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/quickstart Illustrates how to add content to a Livewire component's Blade view file (`resources/views/livewire/counter.blade.php`). This example adds a simple 'Hello World!' heading, maintaining the requirement for a single root HTML element. ```Blade
\n

Hello World!

\n
``` -------------------------------- ### Install PHP Dependencies with Composer Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/contribution-guide Install all required PHP dependencies for the Livewire project using Composer, ensuring all backend components are available for development and testing. ```Shell composer install ``` -------------------------------- ### Example Livewire Component PHP Class Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/traits This is the generated PHP class for a Livewire component. It extends Livewire\Component and defines the render method, which is responsible for returning the component's view. ```php namespace App\Http\Livewire; use Livewire\Component; class Counter extends Component { public function render() { return view('livewire.counter'); } } ``` -------------------------------- ### Livewire Component PHP Class Example Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/quickstart Shows the basic structure of a generated Livewire component PHP class located in `app/Http/Livewire/Counter.php`. It extends `Livewire\Component` and includes a `render` method responsible for returning the component's Blade view. ```PHP namespace App\Http\Livewire;\n\nuse Livewire\Component;\n\nclass Counter extends Component\n{\n public function render()\n {\n return view('livewire.counter');\n }\n} ``` -------------------------------- ### Example of Registering a Livewire JavaScript Hook Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/reference Demonstrates how to register a JavaScript hook using `Livewire.hook()` to tap into specific parts of a Livewire component's JavaScript lifecycle. This example shows the `component.initialized` hook. ```JavaScript Livewire.hook('component.initialized', component => { // }) ``` -------------------------------- ### Adding Content to Livewire Component Blade View Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/turbolinks An example of adding static content to the Livewire component's Blade view, demonstrating the requirement for a single root element. ```HTML

Hello World!

``` -------------------------------- ### Livewire wire:click Directive Example Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/actions Demonstrates the basic usage of the `wire:click` directive to execute a Livewire action when a button is clicked. ```HTML ``` -------------------------------- ### Rendering Livewire Components using Blade Directive Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/artisan-commands Illustrates how to render Livewire components within Laravel Blade files using the `@livewire('component-name')` directive. It shows examples for various component naming conventions, including fully qualified class names. ```Blade @livewire('foo') @livewire('foo-bar') @livewire('foo.bar') @livewire(Package\Livewire\Foo::class) ``` -------------------------------- ### Livewire Template Directive Example Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/reference Demonstrates a basic Livewire template directive for handling click events on an HTML element, triggering a method in the Livewire component. ```HTML ``` -------------------------------- ### Rendering Livewire Components using Tag Syntax (Laravel 7+) Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/artisan-commands Shows the alternative tag syntax for rendering Livewire components, which is available for Laravel 7 and newer versions. This provides a more HTML-like way to embed components. ```Blade ``` -------------------------------- ### Include Livewire Assets in Blade Templates Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/installation Explains how to integrate Livewire's CSS and JavaScript assets into your Laravel Blade templates. It covers both the traditional Blade directive syntax and a more concise tag syntax for including styles and scripts. ```html ... @livewireStyles ... @livewireScripts ``` ```html ... ``` -------------------------------- ### Embed Livewire component and assets in a Blade layout Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/api This example shows how to integrate a Livewire component into any Blade view using its tag-like syntax. It also includes the essential `@livewireStyles` and `@livewireScripts` directives for Livewire to function correctly. ```HTML ... @livewireStyles ... @livewireScripts ``` -------------------------------- ### Display Loading Indicator for Livewire File Uploads Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/file-uploads Example demonstrating how to show a loading indicator using `wire:loading` and `wire:target` while a file is being uploaded via `wire:model` in Livewire. ```html
Uploading...
``` -------------------------------- ### Example Livewire Component PHP Class Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/contribution-guide This is the generated PHP class for a Livewire component, located in `app/Http/Livewire`. It extends `Livewire\Component` and defines a `render` method responsible for returning the associated Blade view. ```PHP namespace App\Http\Livewire; use Livewire\Component; class Counter extends Component { public function render() { return view('livewire.counter'); } } ``` -------------------------------- ### PHP Testing Livewire Components Example Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/reference Demonstrates how to test a Livewire component using Livewire::test() and various assertions like assertDontSee, set, and assertSee to verify component behavior and state. ```PHP public function test() { Livewire::test(ShowPost::class) ->assertDontSee('bar') ->set('foo', 'bar') ->assertSee('bar'); } ``` -------------------------------- ### Publishing Livewire Stubs for Customization Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/artisan-commands Explains how to publish Livewire's default stub files using `php artisan livewire:stubs`. This allows developers to customize the templates used when creating new Livewire component classes and views. ```PHP php artisan livewire:stubs ``` -------------------------------- ### Install JavaScript Dependencies with npm Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/contribution-guide Install all required JavaScript dependencies for the Livewire project using npm, necessary for frontend development and asset compilation. ```Shell npm install ``` -------------------------------- ### Listening for Livewire Browser Events in Frontend Source: https://laravel-livewire.com/docs/2.x/quickstart/events These code examples show how to listen for browser events dispatched by Livewire components in the frontend. The first example uses standard JavaScript's `window.addEventListener`, while the second demonstrates a more concise approach using AlpineJS's event listener syntax directly within HTML. ```JavaScript window.addEventListener('name-updated', event => { alert('Name updated to: ' + event.detail.newName); }) ``` ```HTML
``` -------------------------------- ### Generate Livewire Component using Artisan CLI Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/quickstart Execute this Artisan command to scaffold a new Livewire component, which creates both the PHP class and its corresponding Blade view file. ```Bash php artisan make:livewire counter ``` -------------------------------- ### Livewire Counter Component Initial Blade View Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/reference The initial Blade view file generated for the `Counter` Livewire component. Livewire components require a single root HTML element in their view file. ```Blade
...
``` -------------------------------- ### Example of a Livewire Component Class Lifecycle Hook (PHP) Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/reference Illustrates how to declare a lifecycle method within a Livewire component class in PHP. This example shows the `mount()` method, which is analogous to a constructor and is called when the component is initialized. ```PHP class ShowPost extends Component { public function mount() { // } } ``` -------------------------------- ### Common Livewire Event Directives Examples Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/actions Illustrates the usage of common Livewire directives for listening to various browser events. Examples include `wire:click` for button clicks, `wire:keydown.enter` for specific key presses on input fields, and `wire:submit.prevent` for form submissions with default event prevention. ```html ``` ```html ``` ```html
...
``` -------------------------------- ### Example of an Inline Livewire Component Class Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/making-components This PHP code block illustrates the structure of an inline Livewire component. The 'render()' method returns a Blade template string directly, eliminating the need for a separate '.blade.php' file and consolidating component logic and view into a single file. ```php class ShowPosts extends Component { public function render() { return <<<'blade'
blade; } } ``` -------------------------------- ### Livewire Component PHP Class Definition Example Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/query-string This PHP code defines a basic Livewire component class, `Counter`, which extends `Livewire\Component`. The `render` method is responsible for returning the Blade view associated with this component. ```PHP namespace App\Http\Livewire; use Livewire\Component; class Counter extends Component { public function render() { return view('livewire.counter'); } } ``` -------------------------------- ### Generate a New Livewire Component Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/turbolinks This command uses Artisan to scaffold a new Livewire component, automatically creating both the PHP class file and its corresponding Blade view file. ```Bash php artisan make:livewire counter ``` -------------------------------- ### Generate Livewire Component using Artisan CLI Source: https://laravel-livewire.com/docs/2.x/quickstart/quickstart Run this command to generate a new Livewire component, which creates both the PHP class and its corresponding Blade view file. ```Bash php artisan make:livewire counter ``` -------------------------------- ### Run All Livewire Tests After Setup Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/contribution-guide Execute the `phpunit` command to run all Livewire tests, including PHPUnit unit tests and Dusk browser tests, to verify that the local development environment is correctly configured and all tests pass initially. ```Shell phpunit ``` -------------------------------- ### Livewire Component Blade View with Content Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/deployment This updated Blade view for the Livewire component demonstrates how to add actual content within the required single root HTML element. Here, it displays a simple 'Hello World!' heading. ```Blade

Hello World!

``` -------------------------------- ### Navigate to Livewire Project Directory Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/contribution-guide Change the current working directory in your terminal to the newly cloned Livewire repository, which is necessary before installing dependencies or running commands. ```Shell cd ~/packages/livewire ``` -------------------------------- ### Livewire Component Blade View with Content Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/traits This example shows how to add content to a Livewire component's Blade view. Remember that Livewire components require a single root HTML element. ```blade

Hello World!

``` -------------------------------- ### Livewire Component PHP Class Structure Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/file-downloads This PHP file defines the Livewire component's backend logic. It extends Livewire\Component and includes a render method responsible for returning the component's view. ```php namespace App\Http\Livewire; use Livewire\Component; class Counter extends Component { public function render() { return view('livewire.counter'); } } ``` -------------------------------- ### Generate a new Livewire component using Artisan Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/api Run this Artisan command to scaffold a new Livewire component. It automatically creates the necessary PHP class file in `app/Http/Livewire` and its corresponding Blade view file in `resources/views/livewire`. ```Bash php artisan make:livewire counter ``` -------------------------------- ### Generate Livewire Component using Artisan Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/reference Runs the Laravel Artisan command to create a new Livewire component. This command automatically generates both the PHP class file for the component and its corresponding Blade view file. ```Bash php artisan make:livewire counter ``` -------------------------------- ### Implement Livewire Counter Component Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/deployment Demonstrates how to create a basic interactive counter using Livewire. This includes both the PHP class defining the component's logic (state and actions) and the Blade view for its presentation, showing how to bind UI elements to component methods. ```php use Livewire\Component; class Counter extends Component { public $count = 0; public function increment() { $this->count++; } public function decrement() { $this->count--; } public function render() { return view('livewire.counter'); } } ``` ```blade

{{ $count }}

``` -------------------------------- ### Create Livewire Component using Kebab Notation Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/making-components Livewire supports kebab-case notation for component names, which automatically converts to PascalCase for class names and kebab-case for view file names. This command creates a component named 'show-posts'. ```php php artisan make:livewire show-posts ``` -------------------------------- ### Generate Livewire Component using Artisan Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/file-downloads Use the make:livewire Artisan command to quickly scaffold a new Livewire component. This command creates both the PHP class and its corresponding Blade view file. ```bash php artisan make:livewire counter ``` -------------------------------- ### Include Livewire Component in Blade View Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/deployment Demonstrates how to integrate a Livewire component into a standard Blade view. It shows the placement of Livewire styles, the component itself, and scripts within the HTML structure to enable Livewire functionality. ```html ... @livewireStyles @livewire('counter') ... @livewireScripts ``` -------------------------------- ### Using Laravel's response()->download() for File Downloads Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/file-downloads This PHP snippet shows an alternative method for initiating file downloads within a Livewire component action, leveraging Laravel's `response()->download()` helper to serve a file from a specified path. ```PHP return response()->download(storage_path('exports/export.csv')); ``` -------------------------------- ### Livewire Component Blade View (Initial) Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/file-downloads This Blade file serves as the component's frontend template. Livewire components require a single root HTML element, as shown in this initial generated structure. ```html
...
``` -------------------------------- ### Include Livewire Styles and Scripts in Blade Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/quickstart Add these Blade directives to your main layout file to ensure Livewire's CSS and JavaScript are loaded on pages where Livewire components are used. ```Blade @livewireStyles ... @livewireScripts ``` -------------------------------- ### Directly specify wire:key for Livewire DOM tracking Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/troubleshooting This example demonstrates the direct application of `wire:key` to a DOM element. While powerful for specific tracking issues, over-reliance on this attribute can indicate underlying architectural problems. It explicitly tells Livewire how to identify and manage a particular element. ```HTML
...
...
``` -------------------------------- ### Livewire Counter Component Initial Blade View Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/upgrading This is the initial Blade view file generated for the `Counter` Livewire component. Livewire components require their view files to have a single root HTML element. ```blade
...
``` -------------------------------- ### Testing Livewire File Uploads Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/file-uploads Provides an example of how to write tests for Livewire components that handle file uploads. It leverages Laravel's `Storage::fake()` and `UploadedFile::fake()` helpers to simulate file uploads and assert their existence, demonstrating a complete test case and the necessary component method. ```php /** @test */ public function can_upload_photo() { Storage::fake('avatars'); $file = UploadedFile::fake()->image('avatar.png'); Livewire::test(UploadPhoto::class) ->set('photo', $file) ->call('upload', 'uploaded-avatar.png'); Storage::disk('avatars')->assertExists('uploaded-avatar.png'); } ``` ```php class UploadPhoto extends Component { use WithFileUploads; public $photo; // ... public function upload($name) { $this->photo->storeAs('/', $name, $disk = 'avatars'); } } ``` -------------------------------- ### Livewire Component Blade View with Basic Content Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/query-string An example of adding simple HTML content to a Livewire component's Blade view. It demonstrates the requirement for all component content to be wrapped within a single root HTML element. ```Blade

Hello World!

``` -------------------------------- ### Creating Livewire Components with `make` Command Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/artisan-commands Demonstrates various ways to use the `php artisan make:livewire` command to generate new Livewire components. It covers different naming conventions (kebab-case, PascalCase, dot-notation) and options like `--inline` to create only the class file or `--test` to also generate a test file. ```PHP php artisan make:livewire foo # Creates Foo.php & foo.blade.php php artisan make:livewire foo-bar # Creates FooBar.php & foo-bar.blade.php php artisan make:livewire Foo # Creates Foo.php & foo.blade.php php artisan make:livewire FooBar # Creates FooBar.php & foo-bar.blade.php php artisan make:livewire foo.bar # Creates Foo/Bar.php & foo/bar.blade.php php artisan make:livewire foo --inline # Creates only Foo.php php artisan make:livewire foo --test # Creates Foo.php, foo.blade.php, & FooTest.php ``` -------------------------------- ### Add initial 'Hello World!' content to Livewire component view Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/api This snippet demonstrates how to add basic static content to the Livewire component's Blade view. It highlights the crucial requirement that all Livewire components must have a single root HTML element. ```HTML

Hello World!

``` -------------------------------- ### Create a Basic Livewire Component Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/making-components This command generates a new Livewire component named 'ShowPosts', creating a PHP class file in 'app/Http/Livewire/' and a Blade view file in 'resources/views/livewire/'. ```php php artisan make:livewire ShowPosts ``` -------------------------------- ### Livewire V2: Pagination View Updates and Configuration Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/upgrading Explains the default pagination view change from Bootstrap-4 to Tailwind in Livewire V2. Provides examples for configuring Bootstrap-4 pagination and referencing custom Blade views for pagination links. ```PHP class ShowPosts extends Component { use WithPagination; protected $paginationTheme = 'bootstrap'; ... } ``` ```Blade {{ $posts->links('pagination-links') }} ``` -------------------------------- ### Initial Livewire Component Blade View Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/deployment This is the initial Blade view file generated for a Livewire component. It serves as a placeholder for the component's HTML and CSS, and it's crucial that it contains a single root HTML element. ```Blade
...
``` -------------------------------- ### Livewire JavaScript File Upload API Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/file-uploads Documentation and example usage for Livewire's JavaScript API functions (`@this.upload`, `@this.uploadMultiple`, `@this.removeUpload`) for integrating with third-party file upload libraries or custom upload workflows. ```JavaScript APIDOC: @this.upload(property: string, file: File, successCallback: function, errorCallback: function, progressCallback: function) property: The Livewire component property to bind the file to. file: The File object to upload. successCallback: Function called on successful upload, receives uploaded filename. errorCallback: Function called on upload error. progressCallback: Function called during upload progress, receives event with detail.progress (1-100). @this.uploadMultiple(property: string, files: File[], successCallback: function, errorCallback: function, progressCallback: function) property: The Livewire component property to bind the files to. files: An array of File objects to upload. successCallback: Function called on successful upload. errorCallback: Function called on upload error. progressCallback: Function called during upload progress. @this.removeUpload(property: string, uploadedFilename: string, successCallback: function) property: The Livewire component property from which to remove the file. uploadedFilename: The filename of the file to remove. successCallback: Function called on successful removal. CODE EXAMPLE: let file = document.querySelector('input[type="file"]').files[0] // Upload a file: @this.upload('photo', file, (uploadedFilename) => { // Success callback. }, () => { // Error callback. }, (event) => { // Progress callback. // event.detail.progress contains a number between 1 and 100 as the upload progresses. }) // Upload multiple files: @this.uploadMultiple('photos', [file], successCallback, errorCallback, progressCallback) // Remove single file from multiple uploaded files @this.removeUpload('photos', uploadedFilename, successCallback) ``` -------------------------------- ### Migrating Manual Layout Configuration: ->layout() to ->extends() Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/upgrading If you manually configured a layout for a route in Livewire 1.x using `->layout()`, this method has been moved to `->extends()` and is now placed within the component's `render()` function in Livewire 2.x. This example demonstrates the updated approach. ```PHP // Before Route::livewire('/post', ShowPosts::class) ->layout('layouts.base') ->section('body'); // After class ShowPosts extends Component { public function render() { return view('livewire.show-posts') ->extends('layouts.base') ->section('body'); } } ``` -------------------------------- ### Deleting Livewire Components with `delete` Command Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/artisan-commands Shows how to remove Livewire components using the `php artisan livewire:delete` command. Examples include basic deletion, deleting without a confirmation prompt using `--force`, and removing the component along with its associated test file. ```PHP php artisan livewire:delete foo # Removes Foo.php & foo.blade.php php artisan livewire:delete foo --force # Removes without confirmation prompt php artisan livewire:delete foo --test # Removes Foo.php & foo.blade.php & FooTest.php ``` -------------------------------- ### Generate Livewire Component using Artisan Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/deployment This Artisan command creates a new Livewire component. Running it will generate both the PHP class file for the component logic and its corresponding Blade view file for the component's UI. ```PHP php artisan make:livewire counter ``` -------------------------------- ### Display Livewire File Upload Progress Bar with AlpineJS Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/file-uploads Example demonstrating how to integrate Livewire file upload events with AlpineJS to display a dynamic progress bar. It listens for `livewire-upload-start`, `livewire-upload-finish`, `livewire-upload-error`, and `livewire-upload-progress` events to update the UI. ```HTML
``` -------------------------------- ### Receive Livewire Parameters with Dependency Injection in mount() Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/rendering-components Demonstrates how to inject dependencies into the mount() method of a Livewire component alongside passed parameters, similar to Laravel controllers. ```PHP use \Illuminate\Session\SessionManager; class ShowPost extends Component { public $title; public $content; public function mount(SessionManager $session, $post) { $session->put("post.{$post->id}.last_viewed", now()); $this->title = $post->title; $this->content = $post->content; } ... } ``` -------------------------------- ### Generate Livewire Component using Artisan Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/quickstart Executes the `make:livewire` Artisan command to create a new Livewire component. This command automatically generates both the component's PHP class file and its corresponding Blade view file. ```Bash php artisan make:livewire counter ``` -------------------------------- ### Traditional Laravel Route, Controller, and View Setup Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/rendering-components This snippet illustrates the conventional Laravel approach for defining a route, a controller method, and a Blade view that renders a Livewire component. It serves as a 'before' example to highlight the boilerplate reduced by Livewire's routing helper. ```PHP // Route Route::get('/home', 'HomeController@show'); // Controller class HomeController extends Controller { public function show() { return view('home'); } } // View @extends('layouts.app') @section('content') @livewire('counter') @endsection ``` -------------------------------- ### Generate New Livewire Component with Artisan Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/upgrading This Artisan command generates a new Livewire component. It creates two files: a PHP class file for the component's logic and a corresponding Blade view file for its HTML structure. ```bash php artisan make:livewire counter ``` -------------------------------- ### Defining and Triggering a Basic Livewire Action Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/actions Demonstrates how to define a public method in a Livewire component (PHP) and trigger it from the frontend using the `wire:click` directive on an HTML element. This setup allows for server-side logic execution in response to client-side interactions. ```php class ShowPost extends Component { public Post $post; public function like() { $this->post->addLikeBy(auth()->user()); } } ``` ```html
``` -------------------------------- ### Livewire Artisan CLI Commands Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/reference Lists various `artisan` commands provided by Livewire to streamline common development tasks such as creating, copying, moving, and deleting components, as well as publishing configuration and assets. ```APIDOC 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 ``` -------------------------------- ### Testing Livewire File Uploads with Laravel Helpers Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/file-uploads This PHPUnit test example demonstrates how to test Livewire file uploads. It uses Laravel's `Storage::fake()` to mock the disk and `UploadedFile::fake()->image()` to create a dummy image file. The test then simulates setting the file on the Livewire component and calling a method to store it, finally asserting its existence on the fake disk. ```PHP /** @test */ public function can_upload_photo() { Storage::fake('avatars'); $file = UploadedFile::fake()->image('avatar.png'); Livewire::test(UploadPhoto::class) ->set('photo', $file) ->call('upload', 'uploaded-avatar.png'); Storage::disk('avatars')->assertExists('uploaded-avatar.png'); } ``` -------------------------------- ### Test Livewire Component with Parameters Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/testing Initializes a test for a Livewire component, passing an array of parameters to its constructor or mount method. ```PHP Livewire::test('foo', ['bar' => $bar]); ``` -------------------------------- ### Test Livewire Component Functionality with PHPUnit Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/testing Demonstrates how to create a Livewire component, its corresponding Blade view, and a PHPUnit test suite to verify its core functionality, including data validation, persistence, and redirection. This example covers creating a post, setting initial data, handling validation errors, and asserting redirects. ```PHP class CreatePost extends Component { public $title; protected $rules = [ 'title' => 'required', ]; public function create() { auth()->user()->posts()->create( $this->validate() ); return redirect()->to('/posts'); } } ``` ```HTML
``` ```PHP class CreatePostTest extends TestCase { /** @test */ function can_create_post() { $this->actingAs(User::factory()->create()); Livewire::test(CreatePost::class) ->set('title', 'foo') ->call('create'); $this->assertTrue(Post::whereTitle('foo')->exists()); } /** @test */ function can_set_initial_title() { $this->actingAs(User::factory()->create()); Livewire::test(CreatePost::class, ['initialTitle' => 'foo']) ->assertSet('title', 'foo'); } /** @test */ function title_is_required() { $this->actingAs(User::factory()->create()); Livewire::test(CreatePost::class) ->set('title', '') ->call('create') ->assertHasErrors(['title' => 'required']); } /** @test */ function is_redirected_to_posts_page_after_creation() { $this->actingAs(User::factory()->create()); Livewire::test(CreatePost::class) ->set('title', 'foo') ->call('create') ->assertRedirect('/posts'); } } ``` -------------------------------- ### Default Livewire Component PHP Class Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/quickstart The initial PHP class generated for a Livewire component, extending Livewire\Component and defining the render() method to display its view. ```PHP namespace App\Http\Livewire; use Livewire\Component; class Counter extends Component { public function render() { return view('livewire.counter'); } } ``` -------------------------------- ### Render Livewire Component with Tag Syntax Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/rendering-components Demonstrates the basic way to embed a Livewire component using the HTML-like tag. ```HTML/Blade
``` -------------------------------- ### Configure Temporary File Upload Directory in Livewire Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/file-uploads Example of customizing the temporary directory for Livewire file uploads in `config/livewire.php`. By default, files are uploaded to `livewire-tmp/`. ```php return [ ... 'temporary_file_upload' => [ ... 'directory' => 'tmp', ], ]; ``` -------------------------------- ### Global Livewire JavaScript Object API Reference Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/reference Documents the methods available on the `window.Livewire` object for frontend interaction and customization, including methods for component retrieval, event handling, and lifecycle management. ```APIDOC Livewire Global Object Methods: first(): Get the first Livewire component's JS object on the page find(componentId): Get a Livewire component by it's ID all(): Get all the Livewire components on a page directive(directiveName, (el, directive, component) => {}): Register a new Livewire directive (`wire:custom-directive`) hook(hookName, (...) => {}): Call a method when JS lifecycle hook is fired. onLoad(() => {}): Fires when Livewire is first finished loading on a page onError((message, statusCode) => {}): Fires when a Livewire request fails. You can `return false` from the callback to prevent Livewire's default behavior onPageExpired((response, message) => {}): When the page or session has expired it executes the callback instead of Livewire's page expired dialog emit(eventName, ...params): Emit an event to all Livewire components listening on a page emitTo(componentName, eventName, ...params): Emit an event to specific component name on(eventName, (...params) => {}): Listen for an event to be emitted from a component start(): Boot Livewire on the page (done for you automatically via `@livewireScripts`) stop(): Tear down Livewire from the page restart(): Stop, then start Livewire on the page rescan(): Re-scan the DOM for newly added Livewire components ``` -------------------------------- ### Call Livewire Component Method with Parameters Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/testing Call the "foo" method, and pass the "bar" and "baz" parameters. ```PHP ->call('foo', 'bar', 'baz'); ``` -------------------------------- ### Generate Livewire Component Tests with Artisan Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/testing Details how to automatically generate a PHPUnit test file alongside a new Livewire component using the `--test` flag with the `php artisan make:livewire` command. It also provides an example of the basic test structure generated, including a render assertion. ```Bash php artisan make:livewire ShowPosts --test ``` ```PHP class ShowPostsTest extends TestCase { /** @test */ public function the_component_can_render() { $component = Livewire::test(ShowPosts::class); $component->assertStatus(200); } } ``` -------------------------------- ### Livewire wire:keydown with PageDown Modifier Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/actions An example of using a specific key modifier (`.page-down`) with `wire:keydown` to trigger an action only when the 'PageDown' key is pressed. ```HTML ``` -------------------------------- ### Livewire wire:keydown Directive Example Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/actions Illustrates the `wire:keydown.enter` directive, which triggers a Livewire action when the 'Enter' key is pressed within an input field. ```HTML ``` -------------------------------- ### Clone Livewire Repository Locally Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/contribution-guide Instructions on how to clone your forked Livewire repository from GitHub to a local directory on your machine, preparing it for local development. ```Shell git clone [email protected]:username/livewire.git ~/packages/livewire ``` -------------------------------- ### Default Livewire PHP Component Class Structure Source: https://laravel-livewire.com/docs/2.x/quickstart/quickstart The initial PHP class generated for a Livewire component, extending `Livewire\Component` and defining a `render` method to display its view. ```PHP namespace App\Http\Livewire; use Livewire\Component; class Counter extends Component { public function render() { return view('livewire.counter'); } } ``` -------------------------------- ### Default Livewire Component Blade View Structure Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/turbolinks The initial Blade view file for a Livewire component. Livewire requires that component views have a single root HTML element. ```HTML
...
``` -------------------------------- ### Receive Livewire Parameters via mount() Method Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/rendering-components Illustrates how to manually intercept and process parameters passed to a Livewire component using the mount() lifecycle method. ```PHP class ShowPost extends Component { public $title; public $content; public function mount($post) { $this->title = $post->title; $this->content = $post->content; } ... } ``` -------------------------------- ### Livewire Component: Defining Custom Methods Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/reference Shows an example of a custom method `save()` within a Livewire component, demonstrating how to emit events after an action is completed. ```PHP class PostForm extends Component { public function save() { ... $this->emit('post-saved'); } } ``` -------------------------------- ### Livewire Magic Actions Reference Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/actions A comprehensive table outlining Livewire's built-in magic actions, their syntax, and a brief explanation of their purpose and usage. ```APIDOC Function | Description --- | --- $refresh | Will re-render the component without firing any action $set('*property*', *value*) | Shortcut to update the value of a property $toggle('*property*') | Shortcut to toggle boolean properties on or off $emit('*event*', *...params*) | Will emit an event on the global event bus, with the provided params $event | A *special* variable that holds the value of the event fired that triggered the action. Example usage: `wire:change="setSomeProperty($event.target.value)"` ``` -------------------------------- ### Livewire Counter Component PHP Logic Source: https://laravel-livewire.com/docs/2.x/quickstart/quickstart Updates the Livewire component's PHP class to include a public `count` property and an `increment` method, enabling interactive counter functionality. ```PHP class Counter extends Component { public $count = 0; public function increment() { $this->count++; } public function render() { return view('livewire.counter'); } } ``` -------------------------------- ### Implement Livewire Counter Component (PHP & Blade) Source: https://laravel-livewire.com/docs/2.x/quickstart/1.x/query-string Provides the full implementation for a Livewire 'Counter' component. This includes the PHP class that manages the counter's state and logic, and the corresponding Blade view that renders the UI and handles user interactions via wire:click directives. ```PHP use Livewire\Component; class Counter extends Component { public $count = 0; public function increment() { $this->count++; } public function decrement() { $this->count--; } public function render() { return view('livewire.counter'); } } ``` ```HTML

{{ $count }}

``` -------------------------------- ### Include Livewire Component in Laravel Blade Layout Source: https://laravel-livewire.com/docs/2.x/quickstart/quickstart Demonstrates how to embed a Livewire component into any Blade view using its custom tag, along with including the necessary Livewire styles and scripts. ```Blade ... @livewireStyles ... @livewireScripts ``` -------------------------------- ### Default Livewire Blade View Structure Source: https://laravel-livewire.com/docs/2.x/quickstart/quickstart The initial Blade view file generated for a Livewire component. Livewire components must always have a single root HTML element in their view. ```Blade
...
``` -------------------------------- ### Default Livewire Component PHP Class Source: https://laravel-livewire.com/docs/2.x/quickstart/2.x/turbolinks The initial PHP class generated for a Livewire component. It extends `Livewire\Component` and defines the `render` method responsible for displaying the component's view. ```PHP namespace App\Http\Livewire; use Livewire\Component; class Counter extends Component { public function render() { return view('livewire.counter'); } } ```