Skip to content
Advertisement

htmlspecialchars() when print view

I get the error:

htmlspecialchars() expects parameter 1 to be string, object given

When print the view get the error, I don’t understand what is wrong in my code. I have print and object? How could solve it?

This is my controller:

    foreach($asesorias as $asesoria)
    {
        $asesoriasWithNames = Array
        (
            'comunidad' => Comunidad::getNameComunidad($asesoria->comunidad_id),
            'provincia' => Provincia::getProvinciaName($asesoria->provincia_id),
            'municipio' => Municipio::getMunicipioName($asesoria->municipio_id),
            'place' => $asesoria->place,
            'date' => $asesoria->date,
            'time' => $asesoria->time,
        );            
    }
    //dd($asesoriasWithNames);
    return view('procesar',compact('marker','asesoriasWithNames'));

This is my view:

       @foreach($asesoriasWithNames as $key => $value)
        @if($loop->index % 4 == 0)
            <div class="card-deck">
        @endif
        <div class="card mb-3">
            <img src="/adminlte/img/user4-128x128.jpg" class="card-img-top" alt="...">
            <div class="card-body">

                <p class="card-text">
                    <strong>$key: </strong>{{ $value }}<br>
                <p class="card-text text-center" style="position:absolute;bottom: 0;left: 0;right: 0;">
                    <a href="#" class="btn btn-info">Reservar</a>
                </p>
            </div>
        </div>
        @if($loop->iteration % 4 == 0)
            </div>
        @endif

    @endforeach

This is my dd() print:

 array:6 [▼
  "comunidad" => {#432 ▼
    +"comunidad": "Cantabria"
  }
  "provincia" => {#441 ▼
    +"provincia": "Cantabria"
  }
  "municipio" => {#440 ▼
    +"municipio": "Arredondo"
  }
  "place" => "Centro Comercial Valle Real Calle Alday, S/N, Maliaño"
  "date" => "2020-01-29"
  "time" => "09:00:00"
]

Advertisement

Answer

I changed the function to return a string only and not an object

Before:

public static function getNameComunidad($id)
{
    return DB::table('comunidades')
                        ->where('id', '=', $id)               
                        ->first();
}

Changed:

public static function getNameComunidad($id)
{
    return DB::table('comunidades')
                        ->where('id', '=', $id)               
                        ->first()->comunidad;
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement