Readouble

Livewire v3 セッションプロパティ

セッションプロパティ

Livewireでは、#[Session]アトリビュートを使用することで、ページのリフレッシュや変更を跨いでプロパティの値を簡単に永続化できます。Livewire makes it easy to persist property values across page refreshes/changes using the #[Session] attribute.

コンポーネント内のプロパティに#[Session]を追加すると、Livewireはそのプロパティの値が変更されるたびにセッションに保存します。こうすることで、ページがリフレッシュされたときに、Livewireはセッションから最新の値を取得し、コンポーネントで使用します。By adding #[Session] to a property in your component, Livewire will store that property's value in the session every time it changes. This way, when a page is refreshed, Livewire will fetch the latest value from the session and use it in your component.

#[Session]アトリビュートは、#[Url]アトリビュートに似ています。どちらも同様のシナリオで役立ちます。主な違いは、#[Session]がURLのクエリ文字列を変更せずに値を永続化することです。これは望ましい場合もあれば、そうでない場合もあります。The #[Session] attribute is analogous to the #[Url][/docs/url] attribute. They are both useful in similar scenarios. The primary difference being #[Session] persists values without modifying the URL's query string, which is sometimes desired; sometimes not.

基本的な使い方Basic usage

ここでは、ユーザーが$searchプロパティに保存された文字列で表示する投稿をフィルタリングできるShowPostsコンポーネントを示します。Here's a ShowPosts component that allows users to filter visible posts by a string stored in a $search property:

<?php

use Livewire\Attributes\Session;
use Livewire\Component;
use App\Models\Post;

class ShowPosts extends Component
{
    #[Session] // [tl! highlight]
    public $search;

    protected function posts()
    {
        return $this->search === ''
            ? Post::all()
            : Post::where('title', 'like', '%'.$this->search.'%');
    }

    public function render()
    {
        return view('livewire.show-posts', [
            'posts' => $this->posts(),
        ]);
    }
}

#[Session]アトリビュートが$searchプロパティに追加されているため、ユーザーが検索値を入力した後、ページをリフレッシュしても検索値は永続化されます。$searchが更新されるたびに、その新しい値はユーザーのセッションに保存され、ページロードを跨いで使用されます。Because the #[Session] attribute has been added to the $search property, after a user enters a search value, they can refresh the page and the search value will be persisted. Every time $search is updated, its new value will be stored in the user's session and used across page loads.

warning Warning! パフォーマンスへの影響 Laravelのセッションはリクエストごとにメモリにロードされるため、ユーザーのセッションに多くのデータを保存すると、特定のユーザーに対してアプリケーション全体のパフォーマンスが低下する可能性があります。[!warning] Performance implications Because Laravel sessions are loaded into memory during every request, you can slow down the performance of your entire application for a given user by storing too much in a user's session.

カスタムキーの設定Setting a custom key

[#Session]を使用する場合、Livewireはコンポーネント名とプロパティ名を組み合わせた動的に生成されたキーを使用して、プロパティ値をセッションに保存します。When using [#Session], Livewire will store the property value in the session using a dynamically generated key that consists of the component name combined with the property name.

これにより、コンポーネントインスタンス全体のプロパティが同じセッション値を使用することが保証されます。また、異なるコンポーネントの同じ名前のプロパティが競合しないようにすることも保証されます。This ensures that properties across component instances will use the same session value. It also ensures properties of the same name from different components won't conflict.

Livewireが特定のプロパティに使用するセッションキーを完全に制御したい場合は、key:パラメータを渡すことができます。If you want full control over what session key Livewire uses for a given property, you can pass the key: parameter:

<?php

use Livewire\Attributes\Session;
use Livewire\Component;

class ShowPosts extends Component
{
    #[Session(key: 'search')] // [tl! highlight]
    public $search;

    // ...
}

Livewireが$searchプロパティの値を保存および取得するときに、指定されたキー "search" を使用します。When Livewire stores and retrieves the value of the $search property, it will use the given key: "search".

さらに、コンポーネント内の他のプロパティから動的にキーを生成したい場合は、次の中括弧表記を使用できます。Additionally, if you want to generate the key dynamically from other properties in your component, you can do so using the following curly brace notation:

<?php

use Livewire\Attributes\Session;
use Livewire\Component;
use App\Models\Author;

class ShowPosts extends Component
{
    public Author $author;

    #[Session(key: 'search-{author.id}')] // [tl! highlight]
    public $search;

    // ...
}

上記の例では、$authorモデルのIDが "4" の場合、セッションキーは search-4 になります。In the above example, if the $author model's id is "4", the session key will become: search-4

章選択

パッケージ

設定

バージョン変更
linkv3 linkv2
明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

テストコード表示
両コード表示
Pestのみ表示
PHPUnitのみ表示
OS表示
全OS表示
macOSのみ表示
windowsのみ表示
linuxのみ表示
JSフレームワーク
両フレームワーク
Reactのみ表示
Vueのみ表示
JSのみ表示

(JSが存在しない場合は、他を全表示)

和文変換

対象文字列と置換文字列を半角スペースで区切ってください。(最大5組各10文字まで)

本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作