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
Laravel 5.3 - htmlspecialchars() expects parameter 1 to be string
提问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.php
is 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->website
is empty/blank.
我认为你$user->website
是空的/空白的。
If you look at the url()
helper method, Laravel will return an instanceof UrlGenerator
if $path
is null.
如果你看一下url()
helper方法,Laravel将返回一个实例的UrlGenerator
,如果$path
是零。
So in your case if $user->website
is empty, you'd get UrlGenerator
back and thus your error about htmlspecialchars
getting 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')
修复这个。