Essentials / Turbo Frames

Docs Essentials

Turbo Frames

The Turbo Frame tag that ships with Turbo can be used in your Blade views just like any other HTML tag:

blade
<turbo-frame id="@domid($post, 'create_comment')">
    <p>Loading...</p>
</turbo-frame>

In this case, the @domid() directive is being used to create a DOM ID that looks like create_comment_post_123. Turbo Laravel also ships with a Blade Component that can be used like this:

blade
<x-turbo::frame :id="[$post, 'create_comment']">
    <p>Loading...</p>
</x-turbo::frame>

When using the Blade Component, you don't have to worry about using the @domid() directive or the dom_id() function, as the package handles this automatically. You may also pass a string if you want to use your own DOM ID.

Any other attribute passed to the Blade Component will get forwarded to the underlying <turbo-frame> element, so if you want to turn a Turbo Frame into a lazy-loading Turbo Frame using the Blade Component, you can do it like so:

blade
<x-turbo::frame
    :id="[$post, 'create_comment']"
    :src="route('post.comments.create', $post)"
    loading="lazy"
>
    <p>Loading...</p>
</x-turbo::frame>

This works for any other attribute you want to forward to the underlying element.

Detecting Turbo Frames Requests

You may want to detect in the backend whether a request came from a Turbo Frame. You may use the wasFromTurboFrame() method for that:

php
if ($request->wasFromTurboFrame()) {
    // ...
}

When used like this, the macro will return true if the Turbo-Frame custom HTTP header is present in the request (which Turbo adds automatically), or false otherwise.

You may also check if the request came from a specific Turbo Frame:

php
if ($request->wasFromTurboFrame(dom_id($post, 'create_comment'))) {
    // ...
}