2017-03-01 22 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 | 
+----+--------------+---------+ 

私は上記にコメントしてきたように、私はLaravelでそれを行うことができますどのように$user_id = 45541

| 8 | content8  | 45541 | 

を想定し、行を次取得したいですか?

私がこれまで試したどのような:

$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

変更し、この1

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

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

への注意:モデル名のために、私は単数形使用して、より良いと思います。投稿を投稿に変更する

関連する問題