0

I am creating a Blog Website for my friend which she can Post a Blog, sometimes she will write a book with chapters and I got it all working.

Right now, I put it on different pages Blog and Library.

Does anyone know how can I make one page that will show the most recent activity on her website?

For example, when she will post a blog, it will show there as a recent activity that says she posted a blog and when she will write a book, it will also show there that she wrote a book.

That page will also show the title of the book or blog post.

Thank you!

By the way, I am using Laravel 5.6 and Vue JS

BrokenBinary
  • 7,731
  • 3
  • 43
  • 54
Imperatura
  • 133
  • 2
  • 12

1 Answers1

1

One Line Summary, write an api that will fetch recent activities and use it in VueJs of the page you want to show recent activities.

Details

If you have two separate tables (i.e. models) for blog_post and book_chapter. Then union these two models and get the latest 10 entries. Ultimately these 10 entries are the most recent activities. To get latest 10, you can sort by created_at in desc and limit to 10.

Shafi
  • 1,850
  • 3
  • 22
  • 44
  • i will try this sir and search more about it since I am new to laravel. thanks! – Imperatura Dec 12 '18 at 05:47
  • Comment here, whether it solves or not. If solves the issue don't forget to accept this answer. Check this if not checked already, https://stackoverflow.com/help/someone-answers – Shafi Dec 12 '18 at 05:52
  • I tried it now but it seems like it gets only 2 of each record, I created 3 books and 4 blogs and i used union. it shows only 4 records – Imperatura Dec 12 '18 at 06:31
  • check your query and where constraints – Shafi Dec 12 '18 at 06:33
  • i just used $books = Book::latest()->get(); and the blogs in the same way and then $result = $blogs->union($books); then compact it to show it in the view template – Imperatura Dec 12 '18 at 06:35
  • latest will return the last one. For this you are getting two - one from each model. – Shafi Dec 12 '18 at 06:58
  • Use `take(10)` instead of `latest()` – Shafi Dec 12 '18 at 07:02
  • latest() in laravel will return a collection of data in order from old to latest data. – Imperatura Dec 12 '18 at 07:02
  • @Imperatura it depends how the collection is ordered. If you want from old to new, sort from new to old, take(10) and sort again. – Dimitri Mostrey Dec 12 '18 at 08:58
  • @Dimitri but right now my problem is that.. i have 5 blogs and 5 books.. when i union them I get 7.. 5 blogs and 2 books – Imperatura Dec 12 '18 at 10:03
  • @Imperatura could this be because there is no information on 3 books you are requesting? Empty results/collections will be discarded in a union request as there is nothing to union with. – Dimitri Mostrey Dec 12 '18 at 10:25