I was recently adding some functionality to a moderation queue that I have. Instead of querying one table, Questionnaire, it is now querying Users to so that I can grab their username.
My previous query was just
$questionnaire = Questionnaire::all();
And then when I accessed this on the page I could format it like so:
{{ $questionnaire->created_at->format('F d, Y h:ia') }}
This is how my new query looks in my controller:
public function moderate()
{
$questionnaires = DB::table('users')
->join('questionnaire', 'users.id', '=', 'questionnaire.memberid')
->select('users.username', 'questionnaire.*')
->get();
return view('questionnaire.moderate', ['questionnaires' => $questionnaires]);
}
However when I try to access the created_at
date on the page now I get a Call to a member function format() on a non-object
error. My digging leads me to believe this is because the date isn't converted to a carbon instance.
However all the documentation on Carbon is for when you're just pulling the date and I'm struggling to figure out how to convert it after I get the query results so that I can still access it on the page.
Can anyone hel?
According to the Carbon API, you can create a Carbon instance like so:
$myDate = new Carbon($questionnaire->created_at);
And then just format it the way you want:
{{ $myDate->format('F d, Y h:ia') }}
It may not be exactly what you want, but it'll work.
P.S: your Questionnaire model has the created_at field inside the $dates attribute? I thought that was just enough, even accessing the data the way you are doing it now.