Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Neeraj1005's avatar

htmlSpecialchars() errors

Where did I mistake? I do not understand the error. Error

Facade\Ignition\Exceptions\ViewException
htmlspecialchars() expects parameter 1 to be string, object given (View: C:\wamp64\www\cms\resources\views\admin\userList\userList.blade.php)

This is blade code

                          <td>
                            @if($user->picture!='profile.png')
                            <img src="{{url($user->picture) }}" class="rounded mr-2" alt="User Image" style="width: 50px; height: 50px" /> 
                            @else
                            <span class="medium-badge mr-2">
                              @php 
                              {{
                                $first_char = mb_substr($user->name, 0, 1);
                                $first_char_Capital = strtoupper($first_char);
                                echo $first_char_Capital;
                              }}
                              @endphp
                            </span>  
                            @endif
                          </td> 
0 likes
17 replies
Nakov's avatar

@neeraj1005 it should be in this code I believe:

@php 
{{
    $first_char = mb_substr($user->name, 0, 1);
    $first_char_Capital = strtoupper($first_char);
    echo $first_char_Capital;
}}
@endphp 

So use this instead:

@php 
$first_char = mb_substr($user->name, 0, 1);
$first_char_Capital = strtoupper($first_char);                              
@endphp 

{{ $first_char_Capital }}
Neeraj1005's avatar

@nakov same error comes out

Facade\Ignition\Exceptions\ViewException
htmlspecialchars() expects parameter 1 to be string, object given (View: C:\wamp64\www\cms\resources\views\admin\userList\userList.blade.php)
Nakov's avatar

@neeraj1005 can you show full code please?

Essentially what you are doing wrong is, you are using methods in between {{ }} which is the blade syntax for echoing a string.

So remove all initializations from {{ }} and use @php @endphp for that, or just do all of that in the controller and pass a variable to the view.

Neeraj1005's avatar

@nakov

@extends('admin.layouts.AdminLTE.app')

@section('main-content')

@section('headSection')

<link rel="stylesheet" type="text/css" href="{{ asset('admin/plugins/datatables-bs4/css/dataTables.bootstrap4.css') }}">
<style type="text/css">
            .checkedHighlight{
              background: #f4f4fd!important;
          }
     /*     table tr td{
            border: solid thin red;
        }*/
        </style>

@endsection()

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

  <section class="content-header">
   <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h3>ListofUsers
          <span class="badge badge-secondary" style="font-size: small;">{{$users->count()}}
          </span>
        </h3>
      </div>
      <div class="col-sm-6">
        <ol class="breadcrumb float-sm-right">
          <li class="breadcrumb-item"><a href="#">Home</a></li>
          <li class="breadcrumb-item active">ListofUsers</li>
        </ol>
      </div>
    </div>
  </div><!-- /.container-fluid -->
</section>

    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="card">
        <div class="card-header">
          <h3 class="card-title">Title</h3>

          <div class="card-tools">
            <button type="button" class="btn btn-tool" data-card-widget="collapse" data-toggle="tooltip" title="Collapse">
              <i class="fas fa-minus"></i></button>
            <button type="button" class="btn btn-tool" data-card-widget="remove" data-toggle="tooltip" title="Remove">
              <i class="fas fa-times"></i></button>
          </div>
        </div>
        <div class="card-body">
          {{--Table start--}}
          <table id="example1" class="table table-bordered table-striped">               
                      <thead>
                        <tr>
                          <th width="30">         
                                <div class="custom-control custom-checkbox text-center pl-3">
                                <input type="checkbox" class="checkbox custom-control-input" id="check_all">
                                <label class="custom-control-label ml-2" for="check_all">SelectAll</label>
                                </div>
                            </th>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Email Verification</th>
                            <th>Created</th>
                            <th>Status</th>
                            <th>Action</th>                   
                        </tr>
                      </thead>
                      <tbody>
@forelse($users as $user)
                        <tr>
                          <td><div class="custom-control custom-checkbox">
                <input type="checkbox" class="checkbox custom-control-input" id="{{$user->id}}" data-id="'.$user->id.'">
                <label class="custom-control-label ml-2" for="{{$user->id}}"></label>
                </div></td>
                          <td>
                            @if($user->picture!='profile.png')
                            <img src="{{url($user->picture) }}" class="rounded mr-2" alt="User Image" style="width: 50px; height: 50px" /> 
                            @else
                            <span class="medium-badge mr-2">
                              @php 
                              {{
                                $first_letter = mb_substr($user->name, 0, 1);
                                $first_letter_Capital = strtoupper($first_letter);
                                echo $first_letter_Capital;
                              }}
                              @endphp
                            </span>  
                            @endif
                          </td> 

                          <td><a href="#"> {{$user->email}} </a></td>
                          <td> EmailVerified </td>
                          <td>{{$user->created_at->toDateString()}}</td>
                          <td>Active </td>
                          <td>Action</td>
                        </tr>                       

                @empty

                <td colspan="5" align="center"><h3>No records available</h3></td>

                @endforelse 
                          </tbody>

                          <tfoot>
                            <tr>
                              <td colspan="7">
                                <button class="btn btn-sm btn-outline-secondary font-weight-normal delete-all" data-url="#"><i class="fa fa-trash mr-1" aria-hidden="true"></i>DeleteAll</button> 
                              </td>
                            </tr>
                          </tfoot>

                      </table>{{--Table close--}}      
         
        </div>
        <!-- /.card-body -->
        <div class="card-footer">
          Footer
        </div>
        <!-- /.card-footer-->
      </div>
      <!-- /.card -->

    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

 @endsection()
Nakov's avatar

@neeraj1005 so you haven't changed anything that I've said..

@php 
 {{
 $first_letter = mb_substr($user->name, 0, 1);
$first_letter_Capital = strtoupper($first_letter);
 echo $first_letter_Capital;
}}
@endphp 

throws the error.

Nakov's avatar

@neeraj1005 can you remove that code then, and check if the file will load on the view?

Neeraj1005's avatar

I changed this into this, and same error comes.

                    <td>
                            @if($user->picture!='profile.png')
                            <img src="{{url($user->picture) }}" class="rounded mr-2" alt="User Image" style="width: 50px; height: 50px" /> 
                            @else
                            <span class="medium-badge mr-2">
                              @php 
                              {{
                                $first_letter = mb_substr($user->name, 0, 1);
                                $first_letter_Capital = strtoupper($first_letter);
                              }}
                              @endphp
                              {{$first_letter_Capital}}
                            </span>  
                            @endif
                          </td>
Neeraj1005's avatar

@nakov If I remove this code then it will work properly.

C:\wamp64\www\cms\resources\views/admin/userList/userList.blade.php:83

Laravel error page showing in this line

   @if($user->picture!='profile.png')
Nakov's avatar

@neeraj1005 but please check my reply and you will see what you are doing wrong again

@php 
{{
    $first_letter = mb_substr($user->name, 0, 1);
    $first_letter_Capital = strtoupper($first_letter);
}}
@endphp  

REMOVE THE {{ }} as that's blade, don't mix them:

@php 
$first_letter = mb_substr($user->name, 0, 1);
$first_letter_Capital = strtoupper($first_letter);
@endphp  
Snapey's avatar

{{ }} is blade shortcut for echo (with escape)

change to

                              @php 
                              
                                $first_letter = mb_substr($user->name, 0, 1);
                                $first_letter_Capital = strtoupper($first_letter);
                              
                              @endphp
                              {{$first_letter_Capital}}
Neeraj1005's avatar

@nakov @snapey

                          <td>
                            @if($user->picture!='profile.png')
                            <img src="{{url($user->picture) }}" class="rounded mr-2" alt="User Image" style="width: 50px; height: 50px" /> 
                            @else
                            <span class="medium-badge mr-2">
                              @php
                                $first_letter = mb_substr($user->name, 0, 1);
                                $first_letter_Capital = strtoupper($first_letter);
                              @endphp
                              {{$first_letter_Capital}}
                            </span>  
                            @endif
                          </td> 

Same error occured again

Nakov's avatar

@neeraj1005 you said this

If I remove this code then it will work properly.

Now when you changed it, which line throws the error. I don't think that it is the same part.. Are you using the same wrong syntax somewhere else?

Do you use Ignition, it will tell you exactly where in the view you get the error.

Nakov's avatar

@neeraj1005 so what does your $user->picture returns? Isn't that a string?

I don't think that you have an error there, if you are looking at the compiled view file, than that would be wrong.

If I was debugging the code, I would remove the if statement completely and check if I still get an error :)

jlrdw's avatar

Just the standard reminder here clear those compiled views in between code changes.

That has got me more than once, something was fixed but the compiled view was still loading.

And clear browser cache.

Please or to participate in this conversation.