6

I'm implementing a forum system. The application will have a following system and every user will be able to see activity from their friends. So, when a friend of mine posts something in a forum I should see an item in my feed saying "Your friend replied to forum...".

Every thing is fine 'til here but I want to achieve something like this:

My feed should also have a link to that forum which will take me to the page where my friend's reply is. This page will not be always the last.

So, my question is, if I know the $reply->id of my friend's reply and I'm paginating all the replies of the forum, how can I know in which page is my friend's reply?? Is this easy or I'm asking too much??

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Javier Enríquez
  • 630
  • 1
  • 9
  • 25
  • Did you ever find a solution for this? I'm in the same dilemma with L5 right now. – Winter Feb 20 '15 at 18:41
  • 3
    I just asked the same question - I didn't notice at first that several had already asked it. The forum at Laracasts.com/discuss has this ability but its creator hasn't replied to any of my requests for details on how he accomplished it (probably just busy). Here's my question and some solutions - http://stackoverflow.com/questions/29155512/laravel-5-finding-the-pagination-page-for-a-model – NightMICU Mar 20 '15 at 21:56
  • Maybe you'd have better luck with this than I did, @jeffrey_way on Twitter custom built the laracasts.com forum with this capability. I really wish there was an obvious way to accomplish this in Laravel, it's pretty important – NightMICU Mar 20 '15 at 21:58
  • Thanks!!! I didn't realize there was another question on this topic. I read the answers and I thinks I will go that way for now. – Javier Enríquez Mar 21 '15 at 00:28

1 Answers1

1

It depends on what you are using to sort the replies. This example assumes the user has selected newest and we are using created_at to determine time of posting.

In the feed, to show the page where the reply is

$reply->getLink()

in Reply Model:

public function getLink($per_page = 10)
{
    //assumes ordered by newest
    $newer = $this->forum()->replies()->orderBy('created_at','desc')->where('created_at','>',$this->created_at)->count();
    $page = ceil($newer/$per_page);
   return url(sprintf('forums/%s/replies/%s?per_page=%s&page=%s',$this->forum_id,$this->id, $per_page,$page);
}

If the total posts are 30 and the one you want is the 18th then the link will be

    $newer = 30-18 = 12
    $page = ceil(12/10) = 2

/forums/{id}/replies/{id}?per_page=10&page=2

The one flaw I can see with this method is if the number of posts posted at the exact second are more than the number of replies shown per_page.

ralphowino
  • 529
  • 6
  • 19