Laravel Eloquent get results grouped by days

前端 未结 14 655
一整个雨季
一整个雨季 2020-12-02 06:44

I currently have a table of page_views that records one row for each time a visitor accesses a page, recording the user\'s ip/id and the id of the page itself. I should add

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 07:23

    I know this is an OLD Question and there are multiple answers. How ever according to the docs and my experience on laravel below is the good "Eloquent way" of handling things

    In your model, add a mutator/Getter like this

    public function getCreatedAtTimeAttribute()
    {
       return $this->created_at->toDateString();
    }
    

    Another way is to cast the columns in your model, populate the $cast array

    $casts = [
       'created_at' => 'string'
    ]
    

    The catch here is that you won't be able to use the Carbon on this model again since Eloquent will always cast the column into string

    Hope it helps :)

提交回复
热议问题