Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

php - Laravel 5.3 - htmlspecialchars() expects parameter 1 to be string

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.

this is what my profile.blade.php is made of:

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

Kindly help me solve this problem!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think your $user->website is empty/blank.

If you look at the url() helper method, Laravel will return an instance of UrlGenerator if $path is null.

So in your case if $user->website is empty, you'd get UrlGenerator back and thus your error about htmlspecialchars getting an object.

One simple solution would be to wrap your html chunk with a check:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...