这是我的表结构:
// posts
+----+--------------+---------+
| id | post_content | user_id |
+----+--------------+---------+
| 1 | content1 | 65743 |
| 2 | content2 | 24711 |
| 3 | content3 | 45541 | -- this
| 4 | content4 | 55743 |
| 5 | content5 | 95441 |
| 6 | content6 | 45541 | -- this
| 7 | content7 | 24711 |
| 8 | content8 | 45541 | -- this (I want to select this one, Because it is the last one)
| 9 | content9 | 24711 |
+----+--------------+---------+
正如我在上面评论的那样,我希望获得以下行,假设为$user_id = 45541
| 8 | content8 | 45541 |
我怎样才能在Laravel中做到这一点?
到目前为止我已尝试过:
$last_post = Posts::where('id', $user_id)->OrderBy('id', 'desc')->first();
答案 0 :(得分:1)
你可以这样做:
$last_post = Posts::whereUserId($user_id)->first();
答案 1 :(得分:0)
请修改您的查询,如下所示:
$last_post = Posts::where('id', $user_id)->orderBy('id', 'desc')->first();
按语法正确排序:
Change it From :
OrderBy
To:
orderBy
答案 2 :(得分:0)
更改此
$last_post = Posts::where('id', $user_id)->OrderBy('id', 'desc')->first();
到
$last_post = Posts::where('user_id', $user_id)->orderBy('id', 'desc')->first();
注意:对于型号名称,我认为使用单数更好。将帖子更改为帖子