本文介绍了Laravel 5.3-htmlspecialchars()期望参数1为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是laravel的新手,我很喜欢它.在社交媒体项目上工作时,出现以下错误:htmlspecialchars() expects parameter 1 to be string, object given (View: C:\wamp64\www\histoirevraie\resources\views\user\profile.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:\wamp64\www\histoirevraie\resources\views\user\profile.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">{{ \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>

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

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!

推荐答案

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

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

如果您查看> c3>辅助方法,如果$path为空,则Laravel将返回UrlGenerator instance .

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为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 13:40