6
4

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】リダイレクト処理でビューへ変数を受け渡すときの注意点

Last updated at Posted at 2018-12-29

with() を使うことで、ビューに変数を受け渡すことができます。
ビュー側では {{ $test }} とすることで変数を展開できます。

SampleController.php
class SsampleController extends Controller
{
    public function index()
    {
        return view('sample.index')->with('test', 'テストだよ');
    }
}

with() の書き方は同じですが、下記のようにリダイレクト処理を行なった場合は 'テストだよ' が変数ではなくセッションに保存されるため、 {{ session('test') }} のように取り出すことになります。

SampleController.php
class SsampleController extends Controller
{
    return redirect()->route('sample.index')->with('test', 'テストだよ');
}

{{ $test }} としても、そんな変数は存在しないと怒られてしまいます。

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?