Beforeusing Volt, we recommend getting familiar with standard, class-based Livewire usage. This will allow you to quickly transfer your knowledge of Livewire into writing components using Volt's functional API.
Volt is an elegantly crafted functional API for Livewire that supports single-file components, allowing a component's PHP logic and Blade templates to coexist in the same file. Behind the scenes, the functional API is compiled to Livewire class components and linked with the template present in the same file.
After installing Volt, you may execute the volt:install Artisan command, which will install Volt's service provider file into your application. This service provider specifies the mounted directories in which Volt will search for single file components:
You may create a Volt component by placing a file with the .blade.php extension in any of your Volt mounted directories. By default, the VoltServiceProvider mounts the resources/views/livewire and resources/views/pages directories, but you may customize these directories in your Volt service provider's boot method.
By utilizing Volt's functional API, we can define a Livewire component's logic through imported Livewire\Volt functions. Volt then transforms and compiles the functional code into a conventional Livewire class, enabling us to leverage the extensive capabilities of Livewire with reduced boilerplate.
If you would like to enjoy the single-file component capabilities of Volt while still writing class-based components, we've got you covered. To get started, define an anonymous class that extends Livewire\Volt\Component. Within the class, you may utilize all of the features of Livewire using traditional Livewire syntax:
When using class-based Volt components, the rendered view is the template present in the same file. If you need to pass additional data to the view each time it is rendered, you may use the with method. This data will be passed to the view in addition to the component's public properties:
Sometimes, you may wish to interact with the view instance directly, for example, to set the view's title using a translated string. To achieve this, you may define a rendering method on your component:
The mount function may be used to define the "mount" lifecycle hook of the Livewire component. The parameters provided to the component will be injected into this method. Any other parameters required by the mount hook will be resolved by Laravel's service container:
If the initial value of a state property relies on outside dependencies, such as database queries, models, or container services, its resolution should be encapsulated within a closure. This prevents the value from being resolved until it is absolutely necessary:
Of course, properties may also be declared without explicitly specifying their initial value. In such cases, their initial value will be null or will be set based on the properties passed into the component when it is rendered:
Livewire offers the ability to safeguard properties by enabling you to "lock" them, thereby preventing any modifications from occurring on the client-side. To achieve this using Volt, simply chain the locked method on the state you wish to protect:
When working with nested components, you may find yourself in a situation where you need to pass a property from a parent component to a child component, and have the child component automatically update when the parent component updates the property.
Livewire also allows you to define computed properties, which can be useful for lazily fetching information needed by your component. Computed property results are "memoized", or cached in memory, for an individual Livewire request lifecycle.
Livewire actions provide a convenient way to listen to page interactions and invoke a corresponding method on your component, resulting in the re-rendering of the component. Often, actions are invoked in response to the user clicking a button.
Within the closure, the $this variable is bound to the underlying Livewire component, giving you the ability to access other methods on the component just as you would in a typical Livewire component:
In some cases, your component might declare an action that does not perform any operations that would cause the component's rendered Blade template to change. If that's the case, you can skip the rendering phase of Livewire's lifecycle by encapsulating the action within the action function and chaining the renderless method onto its definition:
Livewire's global event system enables communication between components. If two Livewire components exist on a page, they can communicate by utilizing events and listeners. When using Volt, listeners can be defined using the on function:
If you need to assign dynamic names to event listeners, such as those based on the authenticated user or data passed to the component, you can pass a closure to the on function. This closure can receive any component parameter, as well as additional dependencies which will be resolved via Laravel's service container:
Livewire has a variety of lifecycle hooks that may be used to execute code at various points in a component's lifecycle. Using Volt's convenient API, you can define these lifecycle hooks using their corresponding functions:
When rendering Livewire components, you may pass the lazy parameter to a Livewire component to defer its loading until the initial page is fully loaded. By default, Livewire inserts tags into the DOM where the component will be loaded.
Livewire offers easy access to Laravel's powerful validation features. Using Volt's API, you may define your component's validation rules using the rules function. Like traditional Livewire components, these rules will be applied to your component data when you invoke the validate method:
When using Volt, uploading and storing files is much easier thanks to Livewire. To include the Livewire\WithFileUploads trait on your functional Volt component, you may use the usesFileUploads function:
Sometimes it's useful to update the browser's URL query parameters when your component state changes. In these cases, you can use the url method to instruct Livewire to sync the URL query parameters with a piece of component state:
Like Laravel, Livewire's default pagination view uses Tailwind classes for styling. If you use Bootstrap in your application, you can enable the Bootstrap pagination theme by specifying your desired theme when invoking the usesPagination function:
The view's content is a typical Blade template, including layout definitions and slots. However, by wrapping a portion of the view within the @volt Blade directive, we can convert that piece of the view into a fully-functional Volt component:
Of course, you may declare this data as "state" on your Volt component. When initializing state from data proxied to the component by the view, you only need to declare the name of the state variable. Volt will automatically hydrate the state's default value using the proxied view data:
LiveWire is the best-tasting canned cocktail that you can buy, as rated by San Francisco World Spirits Competition, Consumer Reports, Beverage Testing Institute, and many more. This pack is the best way to try all of our canned cocktails.
We've been named the best at what we do by some of the world's most respected publications and competitions, including Consumer Reports, The Wall Street Journal, The Washington Post, Eater, New York Magazine, San Francisco World Spirits Competition, LA Spirits Awards, and Beverage Testing Institute.
First of all, congrats. We can't wait to meet your little tasteful and responsible drinker in 21.75 years. That said, please don't. There are some incredible non-alcoholic options out there, like this one and this one.
Refunds on alcohol are illegal in most states, with certain exceptions made for defects. If you received product that was damaged or defective, or are otherwise unhappy, please reach out to
in...@drinklivewire.com and we'll see what we can do to help within the scope of the law.
In this example, the model textBody is taking the events fired from the input field as they are bubbled up. Adding wire:ignore to the input prevents livewire from updating that field so that your tinyMCE instance remains the same.
Strip down your app to a playground and get comfortable with how livewire works before trying to jump in a lot of other things. Set up things like a simple form with a simple text input and play around until you get an understanding of livewire. Something simple like:
With Livewire it is possible to develop Blade components that offer a level of reactivity equal to that offered by Vue or React, without the need to manage the complexity of a project with a decoupled frontend and backend. You can continue developing your application within the borders of Laravel and Blade templates.
Livewire is a Composer package that you can add to a Laravel project. It must then be activated on each HTML page (or the page, in case you want to create a Single Page Application) using appropriate Blade directives. Livewire components consist of a PHP class and a Blade file that contains the logic of how a specific frontend component works and it must be rendered.
Livewire installation is absolutely minimal. Install the Composer package in your Laravel project and add the necessary Blade directives to all pages (or to the common layout from which all Blade templates in the project are derived).
In a normal Blade component all public properties of the component class are visible in the blade template. So in $message the value of the $message property will be automatically displayed. In a normal class based component, however, this only happens on the first component rendering. If you type something in the input field nothing changes in the span tag.
The response to each call contains the new rendered HTML for the component, which Livewire will insert into the page in place of the old one. Various custom wire attributes are available. For example you can execute a public method of the component class when clicking on a button. Here is an example of this biding:
3a8082e126