mallorca's avatar

Call relationship in route?

Hi Artisans,

I followed Jeffs series on activity feed. Right now, I have in my route this:

$userevent = User::with('event')->get();

Then in my view, I have something like this:

    @foreach ($userevent as $user)
        @foreach($user->activity as $event)
            @include ("activity.type.{$event->name}")
        @endforeach
    @endforeach

Instead of doing two foreach loops. I would like to refactor it to only have one loop. I would call the $userevent->activity in the route instead and thus control pagination of how many are passed through.

However when I try doing $userevent->activity in the route I get error that it's undefined property. What would be a good practice to call the relationship from the route?

0 likes
5 replies
mallorca's avatar

@Prez Thanks for the reply! The problem is that it returns all the results as it is right now. I would like to paginate the events and was thinking a possible way would be to define the relationship in the route and paginate it from there. Right now, I can only paginate the User in the route, but one user can have 1000 events and they would all be shown.

mallorca's avatar

@Prez Thanks, but I would like to be able to show all the latest activities in this case and paginate them. Do you know if it's possible to reference the relationship in the route like I do in the blade. In that case, what would be a good way of doing it?

mallorca's avatar

That seems to do the trick, didn't think of filtering it on the model view, thanks a lot @Prez !

You mentioned earlier the pagination buttons and loading with ajax. I have one button right now which is going to be like "load more events". Do you perhaps know how I would be able to achieve so that clicking on that button, it "takes" and adds 10 more in the list using your example above? Using ajax / jQuery perhaps? Some code examples would be highly appreciated!

mallorca's avatar

@Prez Thanks for the explanation but I suppose that would be for the other solution, if I wouldn't choose to filter it in the model? Do you perhaps have knowledge/experience of how to achieve ajax loading of more events when clicking on a button in a scenario like this?

mallorca's avatar

@Prez Thanks! One last thing, could you please just provide some code examples on how you would do it in this case? It would mean a lot, I've never made something like that before. Thanks for all your help!

Please or to participate in this conversation.