Laravel nl2br Blade Directive

I think this tutorial, you will learn about laravel nl2br blade directive example.I will do step by step explain laravel nl2br blade. So in this article i will give you a simple example of laravel text area nl2br. Now I’m going to show you about laravel directives nl2br. So just you need just do to some step to done laravel custom directive.

Now In this post,i will give you a simple example of creating custom blade directives in laravel step by step. And you can easily use with laravel 6, laravel 7 and laravel 8 app. Now we will create a @nl2br blade directive and use it with example. we almost need nl2br() for text area value display. you can see bellow layout.

Step 1: Create a Custom Blade Directive

app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
use Blade;

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Paginator::useBootstrap();
Blade::directive('nl2br', function ($string) {
return "<?php echo nl2br($string); ?>";
});
}
}

Step 2: Create Route

Route::get('directive', function () {

$body = '';

if(request()->filled('body')){
$body = request()->body;
}

return view('directive', compact('body'));
});

Read Also : How to Convert String Date to Date Format in PHP?

Step 3: Create Blade File

<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container">
<h1>How to create directive in Laravel? - Codings Point</h1>

<form>
<strong>Enter Something:</strong>
<textarea name="body" class="form-control" style="height: 200px"></textarea>

<button type="submit" class="btn btn-success">Submit</button>
</form>

<p>Body:</p>
<p>@nl2br($body)</p>
</div>

</body>
</html>

Now you can try your own self.

I hope it can help you. Follow us on facebook

Read Also :

How to Get Value of Selected Option In Javascript?

Laravel Pagination with Ajax Example

Laravel 8 Wherein Eloquent Query Example

Increment or decrement column value In Laravel example

--

--

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