Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 echo out session variable containing html in blade

I did a redirect in laravel:

return redirect('admin')->with($returnData);

$returnData is a string that contains a bootstrap info div with the result from the controller. Almost everything is working except when the page loads again it shows the html on the page as if it were text, brackets and everything. If I use this:

@if(!empty(Session::get('error'))) {{ Session::get('error')}} @endif

Then it shows is as pure text. If I change it to

<?php if(!empty(Session::get('error'))) { echo Session::get('error'); } ?>

It works fine. Im ok keeping it like this but I would rather utilize Blade / Laravel as its supposed to be used so I was wondering if there is a way to have the @if statement show the rendered html and not the text version?

like image 631
Webtect Avatar asked Aug 23 '15 22:08

Webtect


3 Answers

I would recommend returning just the error message then in your view create the div. So if you were to change the layout of the view, you would it in one place.

@if(Session::has('error'))
<div class="alert alert-danger">
  {{ Session::get('error')}}
</div>
@endif

hope this help.

like image 63
mdamia Avatar answered Oct 21 '22 10:10

mdamia


To show the rendered HTML you should use {!! $variable->coontent !!} in your view, and this gonna convert your HTML text to render

like image 40
Guillermo Lobos Avatar answered Oct 21 '22 09:10

Guillermo Lobos


May this example will help you. Try this

 @if (session('Error'))
     <div class="alert alert-success">
         {{ session('Error') }}
     </div>
@endif
like image 4
Saeed Awan Avatar answered Oct 21 '22 10:10

Saeed Awan