I can't figure out how to use the querybuilder for a specific need.
We have three tables I'm working with: Tasks Tasks_Comments: Related to Tasks ['tasks.id','tasks_comments.tasks_id'] Attachments: Related to Tasks_Comments ['tasks_comments.id','attachments.bind_id']
I want to list all tasks and then also list information from the attachments table. Since attachments does not have the project it, only the comments id, I'm doing a joinon comments and then one from comments to attachments:
->query(MySQL::type(
DB::table("tasks")->where('tasks.projects_id',$projID)
->leftjoin('tasks_comments', function ($join) {
$join->on('tasks.id','tasks_comments.tasks_id') ;
})
->leftjoin('attachments',function ($join) {
$join->on('tasks_comments.id','attachments.bind_id')
->where('attachments.bind_type','comments');
})
->select('tasks.id')->alias('Task ID')
->addSelect('tasks.name')->alias('Task')
->addSelect('tasks.description')->alias('Description')
->addSelect('attachments.file')->alias('File')
))
->pipe($this->dataStore("client_tasks_by_project_id"));
The problem is that I'm getting one task record per matching comment record even if there is no attachment record match. And I only need one task record per matching attachment.
How can I omit any related comments records that do not have a related attachment record?
Hope this makes some semblance of sense.
TIA,
Rooze