セッションプロパティ
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] 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.
Warning! パフォーマンスへの影響 Laravelのセッションはリクエストごとにメモリにロードされるため、ユーザーのセッションに多くのデータを保存すると、特定のユーザーに対してアプリケーション全体のパフォーマンスが低下する可能性があります。
カスタムキーの設定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