2016-10-14 45 views
7

我是laravel的新手,我很享受它。雖然在社交媒體項目上工作,但我得到了這個錯誤:htmlspecialchars() expects parameter 1 to be string, object given (View: C:\wamp64\www\histoirevraie\resources\views\user\profile.blade.php)Laravel 5.3 - htmlspecialchars()期望參數1是字符串

我檢查了這個網站上的一些問題,但是我還沒有找到解決我的問題的問題。

這就是我的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> 

所有關於用戶的信息由控制器給出:

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

請幫我解決這個問題!

+2

也許'{{}}'之間的某些變量是一個對象?嘗試將所有這些轉儲到控制器中。 – xpuc7o

+1

'$ user-> website'可能是空白的嗎?如果你不給它一個字符串,'url()'輔助方法會給你一個'UrlGenerator'的_instance_。 – jszobody

+0

@jszobody你是對的。發佈您的回答請 – Prince

回答

14

我認爲你的$user->website是空白/空白。

如果你看一下url() helper method,Laravel將返回實例UrlGenerator如果$path爲null。

所以在你的情況下,如果$user->website是空的,你會得到UrlGenerator回來,因此你的錯誤約htmlspecialchars得到一個對象。

一個簡單的解決辦法是用支票來包裝你的HTML塊:

@if($user->website) 
    <li> 
     ... 
    </li> 
@endif 
3

就我而言,我使用的功能葉片文件中像$brand->products()並將其返回數組,這就是爲什麼我所看到的信息。

當我更改我的代碼並返回字符串時,錯誤消失了。

相關問題