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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40045920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:06:13  来源:igfitidea点击:

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

phplaravel-5.3

提问by Prince

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\histtheitroadevraie\resources\views\user\profile.blade.php)

我是 laravel 的新手,我很喜欢它。在进行社交媒体项目时,我遇到了这个错误:htmlspecialchars() expects parameter 1 to be string, object given (View: C:\wamp64\www\histtheitroadevraie\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.

我在这个网站上检查了一些问题,但我没有找到解决我问题的问题。

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

这就是我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!

请帮我解决这个问题!

回答by jszobody

I think your $user->websiteis empty/blank.

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

If you look at the url()helper method, Laravel will return an instanceof UrlGeneratorif $pathis null.

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

So in your case if $user->websiteis empty, you'd get UrlGeneratorback and thus your error about htmlspecialcharsgetting an object.

所以在你的情况下,如果$user->website是空的,你会UrlGenerator回来,因此你关于htmlspecialchars获取对象的错误。

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

一个简单的解决方案是用检查包装您的 html 块:

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

回答by sh6210

In my case, i used a function inside blade file like $brand->products()and it was returning array, thats why i was seeing the message.

就我而言,我在刀片文件中使用了一个函数$brand->products(),它正在返回数组,这就是我看到消息的原因。

when i changed my code and returning string, the error was gone.

当我更改代码并返回字符串时,错误消失了。

回答by brad

I was getting this because in my view I was using $errors->get('username')to show errors but get()returns an array. Switching to $errors->first('username')fixed this.

我得到这个是因为在我看来我是$errors->get('username')用来显示错误但get()返回一个数组。切换到$errors->first('username')修复这个。