Laravel date error

Asked

Viewed 55 times

1

I’m trying to bring the values of the database and present on the screen though he 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 realized that this error is presented when I try to present the value of dates.

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 a 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 idea?

1 answer

2


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

Turns out using Blade, when you do:

{{ $var }}

The value of $var shall be taken over htmlentities PHP and the same expects a string as parameter. Any value that is not string will generate the error. Checking your print_r it is clear that both the field dtemissao as to the dtvencimento are instances of the class DateTime and therefore cannot be done the way it did:

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

To turn these objects into strings, use the method format. I believe that doing something like:

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

Will solve your problem.

For more information, see documentation.

  • I did this test within the function, forgot to perform within the index. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.