2017-07-15 17 views
0

私はLaravel 5.4を使用しています。一時データをテーブルに保存します。例についてはlaravel - 一時データをデータベースに挿入する方法

は私が移行を使用して「プレイヤー」という名前のテーブルを作成しました。

$player = new Player; 

$player->id = '1; 
$player->name = 'NguyenHoang'; 
$player->hero = 'Ringo'; 

$player->save(); 
return view('player'); 

そして、上記のすべてのデータは、私のデータベース内のプレイヤーのテーブルに格納されます:

Players: id, name, hero. 

は、その後、私はその後、私は例えば、雄弁使用してプレイヤーのテーブルにデータを挿入php artisan make:model

を使用してプレーヤーのモデルを作成しました。しかし、私はそれが単なる一時的なデータだと思う。ブラウザを閉じると、すべてのデータが消去されます。

どうすればいいですか?

+0

データはサーバー上(ブラウザではない)にあり、永続的な(データベースであるため)永続的であるため、具体的にこれをコーディングする必要がありますが、おそらくブラウザのonCloseイベントを使用してLaravelにデータを削除するリクエストを送信する –

答えて

1

セッションと呼ばれ、本当にlaravelに固有のものではありません。これは基本的なPHPの原則です。しかし、あなたはフォームのようなものからそれを欲し、データベースにそれを保存しないでください。

// Store a piece of data in the session... 
session(['player' => [ 
'name' => 'NguyenHoang', 
'hero' => 'Ringo']]); 

// Retrieve a piece of data from the session... 
$value = session('player'); 

laravel内のセッションの完全なドキュメントhttps://laravel.com/docs/5.4/session#retrieving-data

上記はあなたが本当に探しているものはおそらくですが、あなたが本当にデータベースで、それは一時的に保存したい場合は、可能性もthatsの:

Driver Prerequisites Database 

When using the database session driver, you will need to create a table to contain the session items. Below is an example Schema declaration for the table: 

Schema::create('sessions', function ($table) { 
    $table->string('id')->unique(); 
    $table->unsignedInteger('user_id')->nullable(); 
    $table->string('ip_address', 45)->nullable(); 
    $table->text('user_agent')->nullable(); 
    $table->text('payload'); 
    $table->integer('last_activity'); }); You may use the session:table Artisan command to generate this migration: 

php artisan session:table 

php artisan migrate 
関連する問題