Hi guys,
SO it's been a long time since I did a Perch build and I'm trying to create a gallery using two collections. One for the gallery 'album' and one for the 'photos'. At the moment I have albums as a related item of photos. At first I thought this made sense but upon building it out, it seems janky - I mean the whole thing isn't ideal as managing such a gallery is going to be far from nice.
So I can make albums, assign a thumbnail etc. Then when I upload photos to the 'photos' collection, I can assign them to an album. The trouble I have is getting my head around setting this up correctly. I always get confused with related things in Perch and it's been a while...
I have my albums collection listing output via a simple template, but when it comes to the details page can I fetch the photos that are related to the albums, because the logic is kind of the opposite way - albums are related to photos. So I am not sure using this logic that I can pull the photos I want that are related to my album. Hopefully some code will help demonstrate my ramblings:
Updated code
- $album = perch_collection('Albums', [
- // from the gallery listing page, we filter and match against the slug to show the detail of the single album
- 'filter' => 'album_slug',
- 'template' => 'gallery/albums/detail',
- 'match' => 'eq',
- 'value' => perch_get('s'),
- 'skip-template' => true,
- // 'return-html' => true,
- // here is where my logic doens't work. I can see ALL the photos coming back in the code
- 'each' => function($photos){
- //assign our related data 'photos' collection. Each Album has photo(s) related to it.
- if(!isset($photos['photos'])) {
- $photos['photos'] = perch_collection('Photos', [
- 'skip-template' => true,
- // 'return-html' => true,
- 'template' => 'gallery/photos/detail',
- 'filter' => 'album._id',
- 'match' => 'eq',
- 'value' => $photos['_id'],
- ]);
- }
- return $photos;
- },
- ]);
This outputs:
- Array
- (
- [0] => Array
- (
- [_id] => 31
- [album_name] => Mats first album
- [_title] => Mats first album
- [album_slug] => mats-first-album
- [album_thumb] => /cms/resources/albums/coast-bg.jpg
- [_sortvalue] => 1000
- [photos] => Array
- (
- [0] => Array
- (
- [_id] => 34
- [photo_image] => /cms/resources/photos/coast-bg.jpg
- [photo_desc] => This image belongs to album 1 and is image #1
- [album] => Array
- (
- [0] => 31
- )
- [_sortvalue] => 1001
- )
- [1] => Array
- (
- [_id] => 35
- [photo_image] => /cms/resources/photos/coast-bg.jpg
- [photo_desc] => This image belongs to album 1 and is image #2
- [album] => Array
- (
- [0] => 31
- )
- [_sortvalue] => 1002
- )
- [2] => Array
- (
- [_id] => 36
- [photo_image] => /cms/resources/photos/coast-bg.jpg
- [photo_desc] => This image belongs to album 1 and is image #3
- [album] => Array
- (
- [0] => 31
- )
- [_sortvalue] => 1003
- )
- )
- )
- )
It looks as if I am successfully getting the images back that belong to this album. From this data, how can I get these passed into my template for the album collection?