Select all the distinct title then paginate them. Find a way to eager load the related files.
By the way, if you really need this because you need a better data structure. It would be so much easier with a title entity which has many files.
Hi everyone,
I am slightly new with Laravel. I have been a member of Laracasts for a while and I have learned a lot here however the companies I worked for never wanted to do anything in Laravel. Thank God I finally started a new job where I can work with Laravel. I am however being faced with a really tricky situation.
The Project is using Laravel 4. The project is basically doing 2 sites, 1 is an API and the second a front end site that feeds off of the API.
We have a table called media_files which is being queried like:
$mediaFiles = $this->entity->filtered()->paginate($this->perPage);
return $this->buildCollection(
$mediaFiles,
$this->mediaFileTransformer
);
They need the results to be grouped by Title and somewhere else they have basically done a foreach loop which creates an array of objects from the collection with the title as its key, like so:
foreach ($mediaFiles['data'] as $file) {
$groupedFiles['data'][$file['title']][] = $file;
}
However of course now the pagination results are all wrong because the original paginate method from the Query builder will be showing the original pagination before the grouping via the loop was done. This is because you might have 3-4 files which have all the same title but different IDs (this is because they will upload different file formats of the same file but want to keep the same title so they can group it. At least that was the idea of the original developer who did this).
My question is if there is a way that I can somehow query the database and get a result that is the same as the loop so that the pagination is correct?
Edit:
We have come up with an SQL query which solves the problem the developers were facing however it is proving to be difficult to get it working. In the DB we can use it and returns about 89K results but I am not being able to do it with Query Builder as it is a crazy query.
SELECT media_files.*, GROUP_CONCAT(media_files.zencoder_job_id SEPARATOR ',') AS children_ids
FROM media_files
GROUP BY media_files.zencoder_job_id,
CASE WHEN media_files.zencoder_job_id IS NULL
THEN media_files.id
ELSE 0
END
Thanks in advance
@pmall Once again thank you so much for your help on this. In the end we looked at the DB structure we inherited from the legacy system and we noticed there was something else we had which could be better used for grouping. The media files had a zencoder_job_id field which was used mainly for the video files that get encoded by Zencoder and this is why initially the developer that started this project was grouping by title (not very good). In the end we went with a query like this:
$orderBy = \DB::table('media_files')->orderBy('id', 'DESC');
// Search Media and then group it by zencoder_job_id where it is not null
$mediaFilesResults = \DB::table(DB::raw("({$orderBy->toSql()}) as sub"))
->whereIn('id', $whereInIds)
->groupBy(\DB::raw("COALESCE(zencoder_job_id, id)"))
->orderBy('id', 'DESC')
->paginate($perPage);
It ended up being that this worked perfectly.
Please or to participate in this conversation.