【问题标题】:Laravel group by day and paginate?Laravel 按天分组并分页?
【发布时间】:2021-04-07 12:31:25
【问题描述】:

我想按天对我的帖子进行分组,按最新排序,并为一个时间轴添加分页,该时间轴还将在前端包含无限滚动的 vue spa:

记录控制器

public function userFeed($userId)
{
    $client = User::where('hashed_id', $userId)->first();   
    $records = Record::where('user_id', $client->id)->latest()->paginate(10);
    return RecordResource::collection($records);
}

记录资源

class RecordResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->hashed_id,
            'owner' => new UserResource($this->owner),
            'title' => $this->title,
            'created_at' => $this->created_at->format('M d Y'),
            'comments' =>  $this->decryptedComments()
        ];
    }
}

我希望 API 的布局便于使用 v-for 循环:

[
    'Sunday' => 5,
    'Monday' => 45,
    'Tuesday' => 452,
    ...
]

<div v-for="(record, index) in records" v-bind:key="index" class="mb-10">
    <div class="card>
        <h1>{{record.title}}</h1>
    </div>
</div>

【问题讨论】:

  • 大部分变化to this thread
  • "message": "stripos() expects parameter 1 to be string, object given",
  • 试试$records = Record::where('user_id', $client-&gt;id)-&gt;latest()-&gt;paginate(10)-&gt;groupBy('created_at');
  • 我仍然遇到同样的错误
  • stripos() 这是不同的错误,它与 group by 无关

标签: laravel vue.js eloquent


【解决方案1】:

如果你想要一个组,则在分页之前使用 if 否则,它不起作用

$records = Record::where('user_id', $client->id)
           ->groupBy(\DB::raw('DATE(created_at) = CURDATE()'))
           ->latest()->paginate()

【讨论】:

    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 2022-12-09
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2019-01-19
    • 1970-01-01
    相关资源
    最近更新 更多