Error date Laravel

1

I'm trying to get the values from the database and display on the screen, however it gives me the following error message:

ErrorException in helpers.php line 531:
htmlentities() expects parameter 1 to be string, object given (View: /var/www/resources/views/contas/index.blade.php)

I noticed that this error is displayed when I try to display the date value.

Function:

public function Index(EntityManagerInterface $em)
{
    $FinContaspagar = $em->getRepository(FinContaspagar::class)->findAll();

     return view('contas.index', [ 'FinContaspagar' => $FinContaspagar ]);
}

Index:

@extends('master')
@section('content')
<div class="row">
    <div class="col-md-12">
        <h3>Contas cadastradas</h3>
        <table class="table table-striped">
            <tr>
                <th>Grupo</th>
                <th>Estabelecimento</th>
                <th>Terceiro</th>
                <th>Codigo</th>
                <th>Data de Emissão</th>
                <th>Data de Vencimento</th>
            </tr>
            @forelse($FinContaspagar as $contas)
                <tr>
                    <td>{{ $contas->getGrupo() }}</td>
                    <td>{{ $contas->getEstabelecimento() }}</td>
                    <td>{{ $contas->getTerceiro() }}</td>
                    <td>{{ $contas->getCodigo() }}</td>
                    <td>{{ $contas->getDtemissao() }}</td>
                    <td>{{ $contas->getDtvencimento() }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="5">Não há ações neste momento!</td>
                </tr>
            @endforelse
        </table>

    </div>
</div>
@endsection

If I give print_r($FinContaspagar) , I get the values:

[0] => ModuloFinanceiro\Entities\FinContaspagar Object
    (
        [grupo:ModuloFinanceiro\Entities\FinContaspagar:private] => 1
        [estabelecimento:ModuloFinanceiro\Entities\FinContaspagar:private] => 1
        [terceiro:ModuloFinanceiro\Entities\FinContaspagar:private] => 1
        [codigo:ModuloFinanceiro\Entities\FinContaspagar:private] => 1
        [dtemissao:ModuloFinanceiro\Entities\FinContaspagar:private] => DateTime Object
            (
                [date] => 2017-04-11 00:00:00
                [timezone_type] => 3
                [timezone] => UTC
            )

        [dtvencimento:ModuloFinanceiro\Entities\FinContaspagar:private] => DateTime Object
            (
                [date] => 2017-04-11 00:00:00
                [timezone_type] => 3
                [timezone] => UTC
            )

    )

Any ideas?

    
asked by anonymous 11.04.2017 / 19:18

1 answer

2

The error message is very clear: values that should be string are objects.

It turns out that using Blade, when you do:

{{ $var }}

The value of $var will be passed to the htmlentities function of PHP and it expects a string as a parameter. Any non-string value will generate the error. Checking your print_r makes it clear that both the dtemissao and dtvencimento fields are instances of the DateTime class and therefore can not be done the way you did:

<td>{{ $contas->getDtemissao() }}</td>
<td>{{ $contas->getDtvencimento() }}</td>

To transform these objects into string, use the format method. I believe doing something like:

<td>{{ $contas->getDtemissao()->format("d-m-Y") }}</td>
<td>{{ $contas->getDtvencimento()->format("d-m-Y") }}</td>

It will solve your problem.

For more information, see the documentation .

    
11.04.2017 / 20:39