Laravel 5.3 - htmlspecialchars()期望参数1为字符串

Pri*_*nce 11 php laravel-5.3

我是laravel的新手,我很享受.在处理社交媒体项目时,我收到此错误:htmlspecialchars() expects parameter 1 to be string, object given (View: C:\wamp64\www\histoirevraie\resources\views\user\profile.blade.php)

我在本网站上查了一些问题,但我没有找到解决问题的问题.

这就是我profile.blade.php的意思:

<ul class="profile-rows">
    <li>
        <span class="the-label">Last visit: </span>
        <span class="the-value mark green">{{ \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $user->lastVisit)->diffForHumans(\Carbon\Carbon::now())}}</span>
    </li>
    <li>
        <span class="the-label">Member since: </span>
        <span class="the-value mark light-gray">{{ $user->created_at->format('F Y') }}</span>
    </li>
    <li>
        <span class="the-label">Profile views: </span>
        <span class="the-value mark light-gray">5146</span>
    </li>
    <li>
        <span class="the-label">Living In: </span>
        <span class="the-value">{{ $user->town }}</span>
    </li>
    <li>
        <span class="the-label">Website: </span>
        <span class="the-value"><a href="{{ url($user->website) }}">{{ $user->website }}</a></span>
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

有关用户的所有信息均由控制器提供:

public function index($username){
        $user = User::where('username', $username)->first();
        return view('user.profile', compact('user'));
    }
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题!

jsz*_*ody 17

我认为你$user->website是空的/空白.

如果你看一下url()helper方法,Laravel将返回一个实例UrlGenerator,如果$path是零.

所以在你的情况下,如果$user->website是空的,你会UrlGenerator回来,因此你htmlspecialchars得到一个对象的错误.

一个简单的解决方案是用一个支票包装你的html块:

@if($user->website)
    <li>
        ...
    </li>
@endif
Run Code Online (Sandbox Code Playgroud)