app/Providers/AppServiceProvider.php
Inside the "boot" method you can define a new blade directive like below.
use Illuminate\Support\Facades\Blade; public function boot() { Blade::directive('br2nl', function ($string) { return "<?php echo nl2br(" . $string . ") ?>"; }); }
Now in your Laravel blade views, you can call the custom directive like below. When you render it in the browser the "\n" will be converted into "<br>" tag.
@br2nl("hello \n world")
It's up to you on how you want the directive to be so play around and share it in the comments below if you have some cool ideas for the community.
Leave a reply