11

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.

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">{{ \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!

4
  • 2
    Maybe some of the variables between {{ }} is a object? Try to dump all of them in the controller. Commented Oct 14, 2016 at 14:51
  • 1
    Is $user->website blank possibly? The url() helper method will give you an instance of UrlGenerator if you don't give it a string. Commented Oct 14, 2016 at 14:59
  • @jszobody you are right. Post your answser please Commented Oct 14, 2016 at 16:09
  • in my case i have given object inside {{ }} Commented Apr 26, 2017 at 11:13

3 Answers 3

18

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
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.