問題描述
我是 Laravel 的新手,我很喜歡它.在處理社交媒體項目時,我收到此錯誤:htmlspecialchars() 期望參數 1 為字符串,給定對象(視圖:C:wamp64wwwhistoirevraie
esourcesviewsuserprofile.blade.php)
I am new to laravel and I am enjoying it. While working on a social media project I got this error: htmlspecialchars() expects parameter 1 to be string, object given (View: C:wamp64wwwhistoirevraie
esourcesviewsuserprofile.blade.php)
我在這個網站上檢查了一些問題,但我沒有找到解決我問題的問題.
I have checked some questions on this site but I have not found a question that solves my problem.
這是我的 profile.blade.php
的組成:
<ul class="profile-rows">
<li>
<span class="the-label">Last visit: </span>
<span class="the-value mark green">{{ CarbonCarbon::createFromFormat('Y-m-d H:i:s', $user->lastVisit)->diffForHumans(CarbonCarbon::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>
有關用戶的所有信息均由控制器提供:
All the information about the user are given by a controller:
public function index($username){
$user = User::where('username', $username)->first();
return view('user.profile', compact('user'));
}
請幫我解決這個問題!
推薦答案
我認為你的 $user->website
是空的/空白的.
I think your $user->website
is empty/blank.
如果您查看 url()
輔助方法,如果 $path
為空,Laravel 將返回 UrlGenerator
的實例.
If you look at the url()
helper method, Laravel will return an instance of UrlGenerator
if $path
is null.
所以在你的情況下,如果 $user->website
是空的,你會得到 UrlGenerator
回來,因此你關于 htmlspecialchars
的錯誤得到一個對象.
So in your case if $user->website
is empty, you'd get UrlGenerator
back and thus your error about htmlspecialchars
getting an object.
一個簡單的解決方案是用檢查包裹你的 html 塊:
One simple solution would be to wrap your html chunk with a check:
@if($user->website)
<li>
...
</li>
@endif
這篇關于Laravel 5.3 - htmlspecialchars() 期望參數 1 為字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!