在SQL VARCHAR中包含新行?

时间:2016-04-22 18:07:20

标签: php sql laravel

在我的网站上,用户可以设置个人资料页面上显示的个人资料说明。当他们更新它时,我希望它包含换行符。

例如,目前:

“嘿,那里! 我的名字叫詹姆斯:)“

保存为“嘿那里!我的名字是詹姆斯:)”

如何让它包含换行符?

顺便说一下,这是我的代码:

  

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');
    }
}

由于

1 个答案:

答案 0 :(得分:2)

在HTML文档中打印文本时,可以使用\n<br>换行符转换为nl2br

例如,在用户模型中创建一个函数。

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

在视图中。

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