donglu8549 2015-08-16 04:27
浏览 44
已采纳

从查询结果将日期转换为碳实例

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?

  • 写回答

1条回答 默认 最新

  • douzhan1994 2015-08-16 04:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路