-1

How to get the remaining time up to the date of consumption ..

{{$item->created_at->toDateString()->diffInDays($item->expired)}}
{{$item->created_at->toDateString() - $item->expired}}
Morteza Negahi
  • 3,045
  • 7
  • 21
  • 37

2 Answers2

2

You are trying to call a function on a string.

See:

$item Instance of \Illuminate\Database\Eloquent\Model

$item->created_at Instance of \Carbon\Carbon

$item->created_at->toDateString() string

$item->created_at->toDateString()->diffInDays() FatalThrowableError Call to a member function diffInDays() on string

Try:

{{ $item->created_at->diffInDays($item->expired) }}


After comment: Your expired attribute is a string and not a date: Tell Eloquent to mutate it as a date:

class Item {
    protected $dates = ['expired'];
}
Hilmi Erdem KEREN
  • 1,730
  • 17
  • 28
  • I have this Error with this way Argument 1 passed to Carbon\Carbon::diffInDays() must be an instance of Carbon\Carbon, string given – Morteza Negahi Sep 23 '16 at 13:23
2
{{ $item->created_at->diffInDays($item->expired) }}
Andy
  • 46,308
  • 56
  • 161
  • 219
Vikash
  • 3,216
  • 2
  • 19
  • 35