Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

Any help with this?

0

First of all, you are passing data to a view incorrectly. See docs on views.

0

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.

Last updated 9 years ago.
0

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();
0

talevskiigor said:

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();

Still no luck. I have to be doing something wrong.

Last updated 9 years ago.
0

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

Last updated 9 years ago.
0

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.

0

@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.

Last updated 9 years ago.
0

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.

0

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.

0

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

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

vinylrain vinylrain Joined 20 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.