iJamesPHP iJamesPHP - 7 months ago 21
SQL Question

Include new lines within SQL VARCHAR?

On my site, users can set a profile description which is displayed on their profile page. When they update it I want it to include line breaks.

For example, currently:

"Hey there!
My name's James :)"

Is saved as "Hey there! My name's James :)"

How can I get it to include the line breaks?

Here's my code by the way:


ProfileController.php


<?php

namespace EG\Http\Controllers;

use Auth;
use Illuminate\Http\Request;
use EG\Models\User;

class ProfileController extends Controller
{
public function getProfile($username)
{
$user = User::where('username', $username)->first();

if (!$user) {
abort(404);
}

return view('profile.index')
->with('user', $user);
}

public function getEdit()
{
return view('profile.edit');
}
public function postEdit(Request $request)
{
$this->validate($request, [
'about' => 'max:250|min:2',
'twitter' => 'alpha_dash|max:15',
'xbl' => 'max:16',
'psn' => 'max:16',
'esea' => 'numeric',
'twitch' => 'alpha_dash|max:25',
]);

Auth::user()->update([
'description' => $request->input('about'),
'twitter' => $request->input('twitter'),
'xbl' => $request->input('xbl'),
'psn' => $request->input('psn'),
'esea' => $request->input('esea'),
'twitch' => $request->input('twitch'),
]);

return redirect()->route('profile.edit')
->with('info', 'Your profile has been updated!')
->with('info_type', 'success');
}
}


Thanks

Answer

You can convert \n line breaks to <br> using nl2br when you print the text in an HTML document.

For example create a function in the user model.

function getDescriptionHTML(){
    return nl2br($this->description);
}

And in the view.

{!! $user->getDescriptionHTML() !!}
Comments