2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】SESSIONに値を渡して取得して表示

Posted at

要約

  1. controllerで\Session::flash('キー名', '渡したい文字列')でセッションに保存
  2. bladeでsession('キー名')で値を取得

環境

Laravel Framework 7.25.0
PHP 7.4.8
MacBook Pro

処理をした後にSESSIONにメッセージを渡して特定の画面に遷移して、その画面上でメッセージを表示したい

たとえば

controller.php
Blog::create($inputs); // データベースに登録
\Session::flash('msg' , '登録しました') // session('msg')に'登録しました'を代入
return redirect()->route('top'); // top画面に遷移(sessionにメッセージが入った状態)

これをトップページで表示するには

top.blade.php
@if(session('msg'))
<p class="msg">
{{ session('msg') }}
</p>
@endif

これを表示したい部分に入れることで
cotroller.phpで\Session::flash('msg' , '登録しました')でセッションに保存した値を表示させることができる!

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?