Laravel - display message data passed to a view

The controller function returns a view and passes such a variable:

return redirect('/projects')->with('message', 'Project created successfully');

Then, on the / projects page, I try to display this message as follows:

@if ( isset( $message ) )


<div class="alert alert-success">

    <ul>

        <li>{{ $message }}</li>

        </ul>

    </div>

@endif

But nothing appears. What am I doing wrong?

+4
source share
4 answers

To display a message, you must use a session:

{{ session('message') }}

You can learn more here.

+3
source

You cannot send variables, but you can scroll some session data.

enter image description here  If you need to store flash data over multiple requests, you can use the reflash method, which will store all flash data for an additional request.

:

$request->session()->put('message', 'project successfully created');
return redirect('/projects');

:

 {{ session('message') }}
+1

To return the message sent in the view to laravel, you must follow the procedure below.

@if($message = Session::get('message')){
<div class="alert alert-success">
   <p>{{$message}}</p>
</div>
}

the session receives the message that you submitted to the view and displaying it on the corresponding page.

for me, if I need to pass a message to a view, I usually do it this way

return redirect()->route('/project')->with('message', 'project successfully created');
0
source

Try the following:

 ... ->with(["message" => "My message"]);
0
source

Source: https://habr.com/ru/post/1692803/


All Articles