我正在构建一个评论应用程序的API,我想首先显示登录用户拥有的评论,我尝试通过检查评论的用户id是否与登录用户的用户id匹配来进行排序,这就是我所拥有的
$comments = Comment::where('project_id', $request->id)
->join('users', 'users.id', '=', 'comments.user_id')
->select('users.name', 'comments.*')
->orderBy('users.id', '=', [Auth::user()->id])
->orderBy('comments.created_at', 'DESC')
->get();发布于 2021-01-13 06:35:35
您可以使用orderByRaw
$comments = Comment::where('project_id', $request->id)
->join('users', 'users.id', '=', 'comments.user_id')
->select('users.name', 'comments.*')
->orderByRaw('IF(users.id = ?,1,0) DESC', [auth()->id()])
->orderBy('comments.created_at', 'DESC')
->get();https://stackoverflow.com/questions/65691578
复制相似问题