wire:loading | Laravel (2024)

Loading indicators are an important part of crafting good user interfaces. They give users visual feedback when a request is being made to the server, so they know they are waiting for a process to complete.

#Basic usage

Livewire provides a simple yet extremely powerful syntax for controlling loading indicators: wire:loading. Adding wire:loading to any element will hide it by default (using display: none in CSS) and show it when a request is sent to the server.

Below is a basic example of a CreatePost component's form with wire:loading being used to toggle a loading message:

<form wire:submit="save">

<!-- ... -->

<button type="submit">Save</button>

<div wire:loading>

Saving post...

</div>

</form>

When a user presses "Save", the "Saving post..." message will appear below the button while the "save" action is being executed. The message will disappear when the response is received from the server and processed by Livewire.

#Removing elements

Alternatively, you can append .remove for the inverse effect, showing an element by default and hiding it during requests to the server:

<div wire:loading.remove>...</div>

#Toggling classes

In addition to toggling the visibility of entire elements, it's often useful to change the styling of an existing element by toggling CSS classes on and off during requests to the server. This technique can be used for things like changing background colors, lowering opacity, triggering spinning animations, and more.

Below is a simple example of using the Tailwind class opacity-50 to make the "Save" button fainter while the form is being submitted:

<button wire:loading.class="opacity-50">Save</button>

Like toggling an element, you can perform the inverse class operation by appending .remove to the wire:loading directive. In the example below, the button's bg-blue-500 class will be removed when the "Save" button is pressed:

<button class="bg-blue-500" wire:loading.class.remove="bg-blue-500">

Save

</button>

#Toggling attributes

By default, when a form is submitted, Livewire will automatically disable the submit button and add the readonly attribute to each input element while the form is being processed.

However, in addition to this default behavior, Livewire offers the .attr modifier to allow you to toggle other attributes on an element or toggle attributes on elements that are outside of forms:

<button

type="button"

wire:click="remove"

wire:loading.attr="disabled"

>

Remove

</button>

Because the button above isn't a submit button, it won't be disabled by Livewire's default form handling behavior when pressed. Instead, we manually added wire:loading.attr="disabled" to achieve this behavior.

#Targeting specific actions

By default, wire:loading will be triggered whenever a component makes a request to the server.

However, in components with multiple elements that can trigger server requests, you should scope your loading indicators down to individual actions.

For example, consider the following "Save post" form. In addition to a "Save" button that submits the form, there might also be a "Remove" button that executes a "remove" action on the component.

By adding wire:target to the following wire:loading element, you can instruct Livewire to only show the loading message when the "Remove" button is clicked:

<form wire:submit="save">

<!-- ... -->

<button type="submit">Save</button>

<button type="button" wire:click="remove">Remove</button>

<div wire:loading wire:target="remove">

Removing post...

</div>

</form>

When the above "Remove" button is pressed, the "Removing post..." message will be displayed to the user. However, the message will not be displayed when the "Save" button is pressed.

#Targeting multiple actions

You may find yourself in a situation where you would like wire:loading to react to some, but not all, actions on a page. In these cases you can pass multiple actions into wire:target separated by a comma. For example:

<form wire:submit="save">

<input type="text" wire:model.blur="title">

<!-- ... -->

<button type="submit">Save</button>

<button type="button" wire:click="remove">Remove</button>

<div wire:loading wire:target="save, remove">

Updating post...

</div>

</form>

The loading indicator ("Updating post...") will now only be shown when the "Remove" or "Save" button are pressed, and not when the $title field is being sent to the server.

#Targeting action parameters

In situations where the same action is triggered with different parameters from multiple places on a page, you can further scope wire:target to a specific action by passing in additional parameters. For example, consider the following scenario where a "Remove" button exists for each post on the page:

<div>

@foreach ($posts as $post)

<div wire:key="{{ $post->id }}">

<h2>{{ $post->title }}</h2>

<button wire:click="remove({{ $post->id }})">Remove</button>

<div wire:loading wire:target="remove({{ $post->id }})">

Removing post...

</div>

</div>

@endforeach

</div>

Without passing {{ $post->id }} to wire:target="remove", the "Removing post..." message would show when any of the buttons on the page are clicked.

However, because we are passing in unique parameters to each instance of wire:target, Livewire will only show the loading message when the matching parameters are passed to the "remove" action.

Multiple action parameters aren't supported

At this time, Livewire only supports targeting a loading indicator by a single method/parameter pair. For example wire:target="remove(1)" is supported, however wire:target="remove(1), add(1)" is not.

#Targeting property updates

Livewire also allows you to target specific component property updates by passing the property's name to the wire:target directive.

Consider the following example where a form input named username uses wire:model.live for real-time validation as a user types:

<form wire:submit="save">

<input type="text" wire:model.live="username">

@error('username') <span>{{ $message }}</span> @enderror

<div wire:loading wire:target="username">

Checking availability of username...

</div>

<!-- ... -->

</form>

The "Checking availability..." message will show when the server is updated with the new username as the user types into the input field.

#Excluding specific loading targets

Sometimes you may wish to display a loading indicator for every Livewire request except a specific property or action. In these cases you can use the wire:target.except modifier like so:

<div wire:loading wire:target.except="download">...</div>

The above loading indicator will now be shown for every Livewire update request on the component except the "download" action.

#Customizing CSS display property

When wire:loading is added to an element, Livewire updates the CSS display property of the element to show and hide the element. By default, Livewire uses none to hide and inline-block to show.

If you are toggling an element that uses a display value other than inline-block, like flex in the following example, you can append .flex to wire:loading:

<div class="flex" wire:loading.flex>...</div>

Below is the complete list of available display values:

<div wire:loading.inline-flex>...</div>

<div wire:loading.inline>...</div>

<div wire:loading.block>...</div>

<div wire:loading.table>...</div>

<div wire:loading.flex>...</div>

<div wire:loading.grid>...</div>

#Delaying a loading indicator

On fast connections, updates often happen so quickly that loading indicators only flash briefly on the screen before being removed. In these cases, the indicator is more of a distraction than a helpful affordance.

For this reason, Livewire provides a .delay modifier to delay the showing of an indicator. For example, if you add wire:loading.delay to an element like so:

<div wire:loading.delay>...</div>

The above element will only appear if the request takes over 200 milliseconds. The user will never see the indicator if the request completes before then.

To customize the amount of time to delay the loading indicator, you can use one of Livewire's helpful interval aliases:

<div wire:loading.delay.shortest>...</div> <!-- 50ms -->

<div wire:loading.delay.shorter>...</div> <!-- 100ms -->

<div wire:loading.delay.short>...</div> <!-- 150ms -->

<div wire:loading.delay>...</div> <!-- 200ms -->

<div wire:loading.delay.long>...</div> <!-- 300ms -->

<div wire:loading.delay.longer>...</div> <!-- 500ms -->

<div wire:loading.delay.longest>...</div> <!-- 1000ms -->

wire:loading | Laravel (2024)

FAQs

What is wire loading? ›

By default, wire:loading will be triggered whenever a component makes a request to the server. However, in components with multiple elements that can trigger server requests, you should scope your loading indicators down to individual actions.

Is Laravel still relevant in 2024? ›

These include a more robust application programming interface (API), improvements in database management and better support for mobile applications. These enhancements will help make Laravel 11 a top choice for web developers in 2024.

How good is Livewire Laravel? ›

Loved by developers around the world. Here's what people are saying about using Livewire. Livewire is a breath of fresh air in a world full of complex build systems, config files, and package manager layer cakes. It is a massive paradigm shift that allows me to build dynamic experiences much faster than ever before.

Why do we use Laravel Livewire? ›

Laravel Livewire allows developers to build dynamic interfaces without writing a single line of JavaScript. This is a significant advantage for those who may not be well-versed in front-end technologies or simply want to streamline their development workflow.

What is a wire load? ›

Put simply, “line” refers to the wires that feed electricity, and “load” refers to the wires that move it along. A line wire carries power from the source to a device or an appliance. The load wire moves that power to the next device on the chain.

How to identify load wire? ›

In the color system, you will see line wires that are black and load wires that are either red, blue, or black. Unfortunately, this means there is a chance either wire may be black, making it pretty confusing if this is your only indicator. However, if you see a wire that is red or blue, it's most likely a load wire.

Does NASA use Laravel? ›

While Laravel does also cover the basics of security it doesn't live up to Django's security level. That's why, for example, NASA uses Django for their website.

Is PHP becoming obsolete? ›

If you have any interest in PHP, you might have heard the famous statistic that PHP fuels 80% of the whole Internet. But how true is it as of today? According to the latest metrics from W3Techs, in 2024, PHP still powers 76.5% of websites where the server-side programming language can be discerned.

Is Laravel outdated? ›

Because the world is undergoing rapid change, there is no need for you to concern yourself with any technology that may soon become obsolete. But the good news is that you won't have to worry about soon Laravel disappears. It's going to expand much farther!

Is Livewire good for big projects? ›

Yes it is. I've used it on fairly large project that provides educational contents for lots of schools. Most of the complaints I read about it is due to people not using some features properly. For example, in a large form it is advisable to use lazy or deferred updating to avoid hitting the server on every keystroke.

Should I use inertia or livewire? ›

If you have a profile more oriented towards back-end development, you will definitely feel more comfortable using Livewire. However, if your team includes people who are already familiar with front-end frameworks like React, Vue, or Angular, then Inertia. js may be the better choice.

Does Laravel have a future? ›

Laravel is a popular PHP framework in 2024 used for web application development. It is known for its elegant syntax, ease of use, and extensive libraries. Laravel has been gaining popularity among developers worldwide, and it is expected to grow even more in 2024.

What are the drawbacks of Laravel Livewire? ›

Learning curve: While staying within Laravel is familiar, understanding the Livewire architecture and how it interacts with the server can have a slight learning curve. Debugging complexity: Debugging can be more complex than traditional Laravel applications due to the server-side rendering and AJAX requests involved.

Is Laravel Livewire safe? ›

Livewire has internal security features to handle many cases, however, there are times when it's up to your application code to keep your components secure.

Is Laravel Livewire SEO friendly? ›

Livewire renders the initial component output with the page (like a Blade include). This way, it's SEO friendly. When an interaction occurs, Livewire makes an AJAX request to the server with the updated data. The server re-renders the component and responds with the new HTML.

What is a cable load? ›

In the electrical trades, the terms "line" and "load" are shorthand words that refer to the electrical wires that deliver power from the source to a device (line), vs. those that carry power onward to other devices further along the circuit (load).

What is the meaning of loaded cable? ›

What is a load wire in electrical wiring? The load line that is the cable through which the power supply supplies power to the electrical equipment, which must consume electrical energy, so the load line is the live wire. BTW, the live wire is the power line that carries electricity in the circuit.

What does wire mean in transaction? ›

Wire transfers move money electronically from one bank account to another. They can be domestic (between two U.S. accounts) or between a U.S. and international account. A wire transfer is a common way to electronically move money from one person to another.

Does the hot wire go to line or load? ›

The circuit's hot wire (typically colored black or red) connects to the black or brass-colored screw terminal marked LINE. The white neutral wire connects to the silver-colored screw terminal marked LINE. The markings for line and load usually are printed on the back of the outlet's plastic body.

Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 5698

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.