Different uses of with() in Laravel
March 06, 2021
In Laravel you can use the with() method to pass data to a view for example.
But there are different ways of this with() method as for how the passed parameters are treated by the system in the view they've been passed to:
Case 1: return redirect, session variable
In the function: return redirect ('sample/page')->with('myMessage', ‘This is my message’);
In the view: {{ session('myMessage') }}
Case 2: return view, variable
In the function: return view ('sample/page')->with('myMessage', ‘This is my message’);
In the view: {{ $myMessage }}