2017-03-01 44 views
-2

这里是我的表结构:如何获得laravel的最后一篇文章?

// 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

什么不适合你的尝试? –

+0

@u_mulder emm我不知道,我还没有测试。 – stack

+2

那么你的问题是什么? –

回答

0

请修改查询象下面这样:

Change it From : 

OrderBy 

To: 

orderBy 
0

你可以这样做:

$last_post = Posts::where('id', $user_id)->orderBy('id', 'desc')->first(); 

由语法正确下订单它是这样的:

$last_post = Posts::whereUserId($user_id)->first(); 
0

变化这一个

$last_post = Posts::where('id', $user_id)->OrderBy('id', 'desc')->first(); 

$last_post = Posts::where('user_id', $user_id)->orderBy('id', 'desc')->first(); 

注:为型号名称,我认为更好的使用单数。更改发布帖子

相关问题