nicolaszein's avatar

Paginate but add linked content

I have a very sticky problem i cannot seem to find a solution to. I have a db that provides me with a paginated list of tasks that it sends back to my vue JS front end via an api.

That works beautifully, however, my data that is displayed in a table is a bunch of tasks that can be children of another todo that has a flag called 'group', meaning that if that task is a group it will have children associated with it. Think of it like a folder with files inside and not tasks sitting at the root level.

So any task that is sent back via pagination that is a group, needs to have all it's children sent back with it as well and i have no idea how to do that and i do not know how it will mess up the pagination.

I might have a set of 200 paginated results that are in a set of 6 for example but in the first set of 200 tasks there are lets say 2 group tasks each containing 30 tasks let's say. That means that i need to retrieve the 200 tasks in the pagination array and attach to that query the additional tasks that have the parent column containing the UID of the group tasks.

Sorry for all that background info, any ideas?

0 likes
9 replies
jlrdw's avatar

Maybe set up double pagination.

Snapey's avatar

load your tasks with the related group tasks.

You have not said ifyou have any relationships setup, or if you are using eloquent?

nicolaszein's avatar

@SNAPEY - Can you point me in the right direction please or provide the syntax i need to use? Yes i am using eloquant for most of my queries. I don't see what kind of relationship i can setup here.

jlrdw's avatar

Double pagination of what? Sorry i am not familiar with that concept.

When displaying master detail relations one master could have only 5 children.

However another master could have 2000 children (details).

An example is a A/R report, one company may have hundreds of owed bills.

So double pagination is just that:

  • A link for next company (master)
  • Pagination links for the details

http://novate.co.uk/using-multiple-pagination-links-on-one-page/

Snapey's avatar
Snapey
Best Answer
Level 122

You have said that the task has a 'parent id' field.

The model can have a relationship back to itself, eg like

Task model

public function subTasks()
{
    return $this->hasMany(Task::class, 'parent_id','id');
}

public function parentTask()
{
    return $this->belongsTo(Task::class, 'parent_id','id');
}

then in the controller

$tasks = Task::with('subTasks')->whereNull('parent_id')->paginate(20);

obviously need to change the terms to match your use case.

nicolaszein's avatar

@SNAPEY - Thank you Snapey,

this is wonderful, i was not aware this was possible.

I don't want to paginate the subtasks though, i have no mechanism in place to do that. They need to be appended to the first set of pagination results.

Is that possible?

Snapey's avatar

The code I show only paginates the top level - not the children?

nicolaszein's avatar

I actually had to go another route that was much more efficient and that was doing my own pagination. It was a lot more work but it's beautiful and i control every aspect of this module now.

Please or to participate in this conversation.