0

On my Controller I have the code:

public function receita(Request $request){
    $id = $request['id'];
    $marcacao = Marcacao::find($id);

    $produtos = ProdutoUnidade::where('unidade_id', $marcacao->agenda->vinculo->unidade->id)
                              ->get();

    $vencimento = new DateTime(date("y-m-d"));
    $vencimento->add(new DateInterval('P90D'));
    $vencimento = $vencimento->format('y-m-d');

    return view('agendas.receita', compact('marcacao', 'produtos', 'vencimento'));
}

In my blade, I have:

@foreach($produtos as $produto)
    <option value="{{$produto->id}}">
        {{$vencimento->diff($produto->produtoNota->dataValidade) > 60 ? '0' : '1'}}
    </option>
@endforeach

I want to know if the difference of $vencimento and $produto->produtoNota->dataValidade is bigger than 60, but I am receive the error:

Call to a member function diff() on string

Any help?

1 Answer 1

1

In your controller you cast DateTime to string with $vencimento = $vencimento->format('y-m-d'); then pass this variable to the view.

So you are calling ->diff on a string. Pass DateTime object to view and format it there if you need to output formated datetime.

P.S. I suggest using carbon instead.

Sign up to request clarification or add additional context in comments.

3 Comments

I comment the line $vencimento = $vencimento->format('y-m-d'); and generate other error: DateTime::diff() expects parameter 1 to be DateTimeInterface, string given
How to use carbon?
I assume you do not cast dates from database to carbon object. See laravel.com/docs/5.7/eloquent-mutators#date-mutators

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.