Hey!
First time writing and I’m trying to filter a collection using both categories and tags.
Right now I’m filtering using categories and my controller looks like this:
<?php
return function ($page, $site) {
$filterBy = get('filter');
$unfiltered = collection('photography')->sortBy(function ($page){
return $page->date()->toDate();}, 'desc');
$projects = $unfiltered
->when($filterBy, function($filterBy){
return $this->filterBy('category', $filterBy);
})
->paginate(16);
$pagination = $projects->pagination();
$filters = $unfiltered->pluck('category', null, true);
$tags = collection('photography')->pluck('tags', ',', true); sort($tags);
return [
'filterBy' => $filterBy,
'unfiltered' => $unfiltered,
'projects' => $projects,
'pagination' => $pagination,
'filters' => $filters,
'tags' => $tags,
];
};
The filter navigation looks like this
<nav class="filter">
<a href="<?= $page->url() ?>">All</a>
<?php foreach ($filters as $filter): ?>
<a href="<?= $page->url() ?>?filter=<?= $filter ?>"><?= $filter ?></a>
<?php endforeach ?>
</nav>
I’m also displaying a list of all the tags using this snippet
<ul class="tags">
<?php foreach($tags as $tag): ?>
<li>
<a href="<?= url('photography', ['params' => ['tag' => $tag]]) ?>">
<?= html($tag) ?>
</a>
</li>
<?php endforeach ?>
</ul>
Here’s a link to the actual page
https://christopherlandin.com/photography
If you click the “List all Genera” button, all tags are shown and the URL’s seems to be correct. But I’m having problems getting the actual filtering to work.
What would be the proper way to implement this in my controller?