Laravel session flash message include html tag link href blade view

Image for post
Image for post

In Laravel you may want to insert html tags like a or strong into the flash message and deliver it to the session as a notification.

Suppose the message you want to show is:

$msg = 'It is done, click here to see the result';

and want to let “click here” accept a link (to the user profile).

Also I suppose you have a route like:

Route::get('user/profile', 'UsersController@show')

It is clear that we should change the message above to include HTML a tag like:

$msg = 'It is done, <a href="'. route('user.profile') . '"> click here  </a>  to see the result';


$msg = 'It is done, <a href="'. url('/user/profile') . '"> click here  </a>  to see the result';

and then in the controller return user to the page you want like“ /home” and deliver the message:

return redirect('/home')->withSuccess($msg);

In the blade file you have to check if the session has the tagged message you want. In our case the message has the success status because of withSuccess($msg). So you should put this in Laravel blade view file:

@if (session()->has('success'))
<div class="alert alert-success">
{!! session()->get('success')!!}

Important note 1:

If the message you deliver has html tag you should use:

unescaped variable which is:

{!! session()->get('success')!!}

and NOT escaped variable:

{{ session()->get('success')}}

It means, Where you display the message, use raw tags {!! !!} around the message instead of {{ }} otherwise the link will not be shown properly!

Important note 2:

The benefits of using HTML-encoding {!! !!} ( unescaped variable) is more important whenever you have & in url or the link which blade will auto convert & into &amp;

Important note 3:

Read the security issue here link. It says:

Be very careful when echoing content that is supplied by users of your application. Always use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.

Thank you for reading! If you enjoyed this article:

Clap it ! Share it! Follow Me in Medium!

Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.

Have a very wonderful day!

Previous Stories You will Love:

Written by

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store