remigis's avatar

return view()->with();

if ($data["errors"]["count"] == 0) {
            return view("counter", $data)->with('success', [
                "Action completed with 0 errors"
            ]);
        }

everything is working, but success message is not displayed. Maybe someone knows why?

0 likes
4 replies
Nakov's avatar

And in the view do you have {{ $success[0] }} because you are adding an array as a value. You should do this instead:

return view("counter", $data)->with('success', 'Action completed with 0 errors');

But you should also make sure that this is the only place you return the view otherwise you will need to check if the $success is set.

@if(isset($success))
{{ $success }}
@endif
1 like
remigis's avatar

view is something like this

@if(session('success'))
@foreach(session('success') as $success)
            <script>
                    toastr.success("{{ $success }}");
                 </script>
@endforeach
@endif

Message is displayed when i do this.

return redirect()->back()->with('success', ["message"]);

But when I use view()->with() message is not displayed.

Nakov's avatar
Nakov
Best Answer
Level 73

Yes, because when you use with on a Response object it adds to a flash session, but when used on a view, it passes it as a parameter as I've shown you above. You can open the underlying functions in your IDE and see how they are implemented.

One is in Iluminate\Http\RedirectResponse and the other in Illuminate\View\View. Same name for the method, different implementation.

If you want to keep using the session, then add this before you return the view:

session()->flash('success', 'Action completed with 0 errors');
return view("counter", $data);
1 like

Please or to participate in this conversation.