在我的网站上,用户可以设置配置文件说明,该描述显示在他们的配置文件页面上。当他们更新它时,我希望它包含换行符。
例如,目前:
"嘿!我叫詹姆斯·:)"
保存为"嘿!我叫詹姆斯·:)"
如何让它包含换行符?
顺便说一下,这是我的代码:
配置文件控制器.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');
}
}
谢谢
在
HTML 文档中打印文本时,可以使用nl2br
将'n
换行符转换为<br>
。
例如,在用户模型中创建一个函数。
function getDescriptionHTML(){
return nl2br($this->description);
}
而在视图中。
{!! $user->getDescriptionHTML() !!}