Tuesday, February 28, 2017

How to get last post in laravel?

Here is my table structure:

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

As I've commented in above, I want to get following row, assuming $user_id = 45541

| 8  | content8     | 45541   |

How can I do that in Laravel?

What I've tried so far:

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




via stack

Advertisement