How do I preserve whitespace when entering data to a database

409 Views Asked by At

I am developing a web application in PHP using the Laravel framework. One of the tables in my database is an 'Articles' table which has a content field. A user of the website enters text into a text area and this is inserted into a database using the following Laravel code:

$article = new Article;
$article->content = $request->content;
....other fields
$article->save();

This works fine. The article is inserted into the database and I can view it. Everything is grand. However, when the article is entered into the database, it does not recognise whitespace (i.e. I can't insert seperate paragraphs, it just joins them all together and displays them as one long paragraph).

I am aware of the nl2br function. Am I suppose to use nl2br BEFORE I save the Article to the database or am I only supposed to use it when outputting. I have followed the documentation that is available but it hasn't worked for me. Are there any other solutions?

Thanks for your help.


SOLVED

It was Laravel itself I think that caused a bit of problems with nl2br function. To display it correctly in a Laravel Blade PHP page is like so

{!! nl2br(e($article->content)) !!}

Thanks for your help guys.

0

There are 0 best solutions below