-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,305
  • 8
  • 24
  • 42

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,949
  • 20
  • 29
  • 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
  • 49,085
  • 60
  • 166
  • 233
Vikash
  • 3,391
  • 2
  • 23
  • 39