LoginSignup
0
0

More than 5 years have passed since last update.

laravel5.4でリダイレクト後にpostの値やメッセージを表示する

Last updated at Posted at 2018-11-07

やりたいこと

登録処理などの完了後やエラー時にリダイレクトしてメッセージやpostされた値を表示したい

コード

myController.php
public function postData(Request $request){
  //postされた内容
  $text = $request->input('text');
  //直後のHTTPリクエストの間だけセッションにpostされた$textを保存します
  $request->session()->flash('posttext', $text);
  //メッセージも同様に保存します
  $request->session()->flash('message', '登録完了しました。');
  //リダイレクトします
  return redirect('/redirect/path/');
}

表示する際は以下のようにします。

myContents.blade.php
@if(Session::has('message'))
<p>{{ session('message') }}</p>
@endif
@if(Session::has('posttext'))
<p>{{ session('posttext') }}</p>
@endif

参考:Laravel 5.4 HTTPセッション

0
0
2

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
0
0