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 }} としても、そんな変数は存在しないと怒られてしまいます。