Maybe set up double pagination.
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?
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.
Please or to participate in this conversation.