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

I am new to laravel and I like it. While working on a project on social networks, 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 checked a few questions on this site, but I did not find a question that solves my problem.

this is what my profile.blade.php consists of:

 <ul class="profile-rows"> <li> <span class="the-label">Last visit: </span> <span class="the-value mark green">{{ \Carbon\Carbon::createFromFormat('Ymd 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 user information is provided by the controller:

 public function index($username){ $user = User::where('username', $username)->first(); return view('user.profile', compact('user')); } 

Please help me solve this problem!

+11
source share
4 answers

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

If you look at url() , Laravel will return an UrlGenerator instance if $path is null.

So, in your case, if $user->website empty, you will get UrlGenerator back and therefore your error regarding htmlspecialchars getting the object.

One simple solution would be to wrap the html check with validation:

 @if($user->website) <li> ... </li> @endif 
+15
source share

In my case, I used a function inside the blade file of the file, for example $brand->products() , and it returned an array, so I saw the message.

when I changed my code and returned the line, the error disappeared.

+4
source share

I got this because, in my opinion, I used $errors->get('username') to show errors, but get() returns an array. Switching to $errors->first('username') fixed this.

0
source share
  1. By checking your HTML form and the data you entered, you send the data of the array, so they give this error, so you send the data in a string format, so convert this data to an array and then send it to it using the post method.
0
source share

All Articles