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

前端 未结 3 1544
执笔经年
执笔经年 2020-12-06 11:38

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 (

相关标签:
3条回答
  • 2020-12-06 12:02

    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
    
    0 讨论(0)
  • 2020-12-06 12:12

    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.

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

    0 讨论(0)
  • 2020-12-06 12:18

    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.

    0 讨论(0)
提交回复
热议问题