I have a site with Projects
, where each Project
has a number or related Pages fields to link to Associates
.
To show the reverse relationship on the Associates pages (i.e. list any Project where that Associate is linked), I’m using the following model:
site/models/associate.php
class AssociatePage extends Page
{
public function projects()
{
$page = $this;
$projects = Pages()->index()->template('project')->filter(function ($project) use ($page) {
return $project->client()->toPages()->has($page)
|| $project->agency()->toPages()->has($page)
|| $project->illustrator()->toPages()->has($page)
|| $project->designer()->toPages()->has($page)
|| $project->photographer()->toPages()->has($page);
});
return $projects;
}
}
On the template:
site/templates/associate.php
<?php
$something = $pages->index(); // why does this need to be here for model to work?
?>
<?php foreach ($page->projects() as $p) : ?>
<?= $p->title(); ?>
<?php endforeach; ?>
If I don’t call that unused variable at the top of the template, I get no results from that model function. If it’s there, it works…
Am I accessing the Pages()
collection correctly in the model? (as the $pages() variable isn’t available in models)