2017-03-01 93 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(); 

注:爲型號名稱,我認爲更好的使用單數。更改發佈帖子

相關問題