Xum said:
First of all, you are passing data to a view incorrectly. See docs on views.
The data gets passed through just fine...
I've had to do it like this:
Due {{ \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $due_date)->diffForHumans() }}
Apparently, Eloquent does not want to make my dates Carbon objects. Why? I still do not know.
You need to declare due_date
in array of $dates in the Eloquent model.
Take a look at Illuminate\Database\Eloquent\Model;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = array();
talevskiigor said:
You need to declare
due_date
in array of $dates in the Eloquent model.
Take a look atIlluminate\Database\Eloquent\Model;
/**
- The attributes that should be mutated to dates.
- @var array */ protected $dates = array();
Still no luck. I have to be doing something wrong.
can you please show your route to the show method?
you can try passing id instead of object
public function show($id){
$work = Work::find($id);
return view('viewname' , compact($work));
}
then on your view you can use
$work->due_date->diffForhumans() instead of $due_date->diffforhumans
cemleme said:
can you please show your route to the show method?
you can try passing id instead of object
public function show($id){ $work = Work::find($id); return view('viewname' , compact($work)); }
then on your view you can use
$work->due_date->diffForhumans() instead of $due_date->diffforhumans
I now do that since there are multiple works, but it still does not work.
@vinylrain,
cemleme's code does work for single Work
object. Could you show how you are getting multiple works, and how you are passing them to the view?
The date-to-Carbon conversion works through accessor functions of eloquent models, so in your view, when you write $work->due_date->diffForHumans()
the $work
object has to be of Work
class.
Double-check that you haven't already created a specific accessor for the field in question. If there is a getDueDateAttribute()
method that you forgot you created (like I did), Eloquent will ignore the fact that it's in your $dates
array.
if you have a collection or Work classes then the above won't work
check its a object not a collection of objects or a array of attributes.
I got same problem and was looking forward for the answer, but at the end I found it for mine
just make sure the $due_date
is not null
when you are trying to call the $due_date->diffForHumans()
hope it solved the problem
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community