Laravel 5.6 - Grab message from Controller into view









up vote
0
down vote

favorite












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










share|improve this question























  • Have you checked what $msg exactly contains? Try dumping it in the controller to see whether it is an array, an object, a string,......
    – Nico Haase
    Nov 9 at 14:53










  • @NicoHaase i tried to dump it var_dump($msg) inside template but still got the same error
    – danielarend
    Nov 9 at 15:14














up vote
0
down vote

favorite












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










share|improve this question























  • Have you checked what $msg exactly contains? Try dumping it in the controller to see whether it is an array, an object, a string,......
    – Nico Haase
    Nov 9 at 14:53










  • @NicoHaase i tried to dump it var_dump($msg) inside template but still got the same error
    – danielarend
    Nov 9 at 15:14












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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










share|improve this question















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







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 14:48

























asked Nov 9 at 14:09









danielarend

326312




326312











  • Have you checked what $msg exactly contains? Try dumping it in the controller to see whether it is an array, an object, a string,......
    – Nico Haase
    Nov 9 at 14:53










  • @NicoHaase i tried to dump it var_dump($msg) inside template but still got the same error
    – danielarend
    Nov 9 at 15:14
















  • Have you checked what $msg exactly contains? Try dumping it in the controller to see whether it is an array, an object, a string,......
    – Nico Haase
    Nov 9 at 14:53










  • @NicoHaase i tried to dump it var_dump($msg) inside template but still got the same error
    – danielarend
    Nov 9 at 15:14















Have you checked what $msg exactly contains? Try dumping it in the controller to see whether it is an array, an object, a string,......
– Nico Haase
Nov 9 at 14:53




Have you checked what $msg exactly contains? Try dumping it in the controller to see whether it is an array, an object, a string,......
– Nico Haase
Nov 9 at 14:53












@NicoHaase i tried to dump it var_dump($msg) inside template but still got the same error
– danielarend
Nov 9 at 15:14




@NicoHaase i tried to dump it var_dump($msg) inside template but still got the same error
– danielarend
Nov 9 at 15:14












2 Answers
2






active

oldest

votes

















up vote
3
down vote













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






share|improve this answer






















  • It didn't wok for me, same error. Can't change controller structure. I tried also $msg->text and error persist.
    – danielarend
    Nov 9 at 14:46










  • Then you have to iterate over the session in order to access your items. Look at my Edit above
    – nakov
    Nov 9 at 14:53










  • Thanks a lot, but still not working, Error message now is 'Illegal string offset 'text''
    – danielarend
    Nov 9 at 15:09










  • try to var_dump the content of the $msg within the view and see what it has, something is missing there so I cannot debug it when I don't have the code.
    – nakov
    Nov 9 at 16:04

















up vote
0
down vote













Do this instead



return redirect('/myroute')->with('success','It worked');


Then on your view



session('success')





share|improve this answer








New contributor




Paul Mikki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • Unfornunatelly i can't change controller structure
    – danielarend
    Nov 9 at 15:10










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227294%2flaravel-5-6-grab-message-from-controller-into-view%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













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






share|improve this answer






















  • It didn't wok for me, same error. Can't change controller structure. I tried also $msg->text and error persist.
    – danielarend
    Nov 9 at 14:46










  • Then you have to iterate over the session in order to access your items. Look at my Edit above
    – nakov
    Nov 9 at 14:53










  • Thanks a lot, but still not working, Error message now is 'Illegal string offset 'text''
    – danielarend
    Nov 9 at 15:09










  • try to var_dump the content of the $msg within the view and see what it has, something is missing there so I cannot debug it when I don't have the code.
    – nakov
    Nov 9 at 16:04














up vote
3
down vote













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






share|improve this answer






















  • It didn't wok for me, same error. Can't change controller structure. I tried also $msg->text and error persist.
    – danielarend
    Nov 9 at 14:46










  • Then you have to iterate over the session in order to access your items. Look at my Edit above
    – nakov
    Nov 9 at 14:53










  • Thanks a lot, but still not working, Error message now is 'Illegal string offset 'text''
    – danielarend
    Nov 9 at 15:09










  • try to var_dump the content of the $msg within the view and see what it has, something is missing there so I cannot debug it when I don't have the code.
    – nakov
    Nov 9 at 16:04












up vote
3
down vote










up vote
3
down vote









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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 14:54

























answered Nov 9 at 14:12









nakov

1,17878




1,17878











  • It didn't wok for me, same error. Can't change controller structure. I tried also $msg->text and error persist.
    – danielarend
    Nov 9 at 14:46










  • Then you have to iterate over the session in order to access your items. Look at my Edit above
    – nakov
    Nov 9 at 14:53










  • Thanks a lot, but still not working, Error message now is 'Illegal string offset 'text''
    – danielarend
    Nov 9 at 15:09










  • try to var_dump the content of the $msg within the view and see what it has, something is missing there so I cannot debug it when I don't have the code.
    – nakov
    Nov 9 at 16:04
















  • It didn't wok for me, same error. Can't change controller structure. I tried also $msg->text and error persist.
    – danielarend
    Nov 9 at 14:46










  • Then you have to iterate over the session in order to access your items. Look at my Edit above
    – nakov
    Nov 9 at 14:53










  • Thanks a lot, but still not working, Error message now is 'Illegal string offset 'text''
    – danielarend
    Nov 9 at 15:09










  • try to var_dump the content of the $msg within the view and see what it has, something is missing there so I cannot debug it when I don't have the code.
    – nakov
    Nov 9 at 16:04















It didn't wok for me, same error. Can't change controller structure. I tried also $msg->text and error persist.
– danielarend
Nov 9 at 14:46




It didn't wok for me, same error. Can't change controller structure. I tried also $msg->text and error persist.
– danielarend
Nov 9 at 14:46












Then you have to iterate over the session in order to access your items. Look at my Edit above
– nakov
Nov 9 at 14:53




Then you have to iterate over the session in order to access your items. Look at my Edit above
– nakov
Nov 9 at 14:53












Thanks a lot, but still not working, Error message now is 'Illegal string offset 'text''
– danielarend
Nov 9 at 15:09




Thanks a lot, but still not working, Error message now is 'Illegal string offset 'text''
– danielarend
Nov 9 at 15:09












try to var_dump the content of the $msg within the view and see what it has, something is missing there so I cannot debug it when I don't have the code.
– nakov
Nov 9 at 16:04




try to var_dump the content of the $msg within the view and see what it has, something is missing there so I cannot debug it when I don't have the code.
– nakov
Nov 9 at 16:04












up vote
0
down vote













Do this instead



return redirect('/myroute')->with('success','It worked');


Then on your view



session('success')





share|improve this answer








New contributor




Paul Mikki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • Unfornunatelly i can't change controller structure
    – danielarend
    Nov 9 at 15:10














up vote
0
down vote













Do this instead



return redirect('/myroute')->with('success','It worked');


Then on your view



session('success')





share|improve this answer








New contributor




Paul Mikki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • Unfornunatelly i can't change controller structure
    – danielarend
    Nov 9 at 15:10












up vote
0
down vote










up vote
0
down vote









Do this instead



return redirect('/myroute')->with('success','It worked');


Then on your view



session('success')





share|improve this answer








New contributor




Paul Mikki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









Do this instead



return redirect('/myroute')->with('success','It worked');


Then on your view



session('success')






share|improve this answer








New contributor




Paul Mikki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




Paul Mikki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered Nov 9 at 14:17









Paul Mikki

583




583




New contributor




Paul Mikki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Paul Mikki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Paul Mikki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Unfornunatelly i can't change controller structure
    – danielarend
    Nov 9 at 15:10
















  • Unfornunatelly i can't change controller structure
    – danielarend
    Nov 9 at 15:10















Unfornunatelly i can't change controller structure
– danielarend
Nov 9 at 15:10




Unfornunatelly i can't change controller structure
– danielarend
Nov 9 at 15:10

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227294%2flaravel-5-6-grab-message-from-controller-into-view%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus