Reputation: 1427
I'm starting with Laravel and i need to show the output of a post request into the view. My controller file returns an array with a message, like this:
return redirect('/myroute')
->with('message', [
'type' => 'success',
'text' => 'It works'
]);
In my view file, i'm trying to grab the message text, but no success. See my code below
@if(Session::has('message'))
{{ $msg = Session::get('message') }}
<h4>{{ $msg->text }}</h4>
@endif
The point is: The condition works, if i changed the {{$msg->text}} to any text it works, but when i try to get the message text, it returns an error:
htmlspecialchars() expects parameter 1 to be string, array given
So, any help is apreciated. If more information is needed, just ask.
PS: i checked this question, but no success at all EDITED: PS2: Can't change controller structure
Upvotes: 1
Views: 206
Reputation: 14288
Try accessing the array as follows:
<h4>{{ $msg['text'] }}</h4>
or just pass an array with the items
->with([
'type' => 'success',
'text' => 'It works'
]);
//in the view
@if(session()->has('text'))
<h4> {{ session('text') }} </h4>
@endif
-- EDIT
iterate than over the session like so:
@foreach (Session::get('message') as $msg)
{{$msg['text']}}
@endforeach
you can read more about that here
Upvotes: 4
Reputation: 83
Do this instead
return redirect('/myroute')->with('success','It worked');
Then on your view
{{session('success')}}
Upvotes: 0