Page with multi relationship to your project module - How to apply sort_order in view.
Created 7 years ago by william

I have created a projects module. What this module allows me to do, is to add projects i have worked on in the past. It has a rich text field and image field (as example).

I have then set up a multi rel field, between the homepage and this module to be able to select the projects i would like featured on the homepage.

To render the list in my homepage view i simply do:

{% for project in page.featured_projects %}
{{ project.title }}
{% endfor %}

What i would like to achieve, is to utilise the sort_order of my projects. I basically need an orderBy. How would i go about this the best "pyro" way? Would it be to create a projects critera? In that case i am a little uncertain how to get the page rel.

Would appreciate some input.


william  —  7 years ago

I quick and dirty(?) solution :

I created a plugin, which returns an array of the projects ids related to this page.
Then i ordered them using entries.

{% set ids = get_featured_projects_ids(page.featured_projects) %}
{% set results = entries('projects', 'projects').orderBy('sort_order','ASC').find(ids) %}

But i think i would like a better solution. Any help is appreciated.


ryanthompson  —  7 years ago

Try this:

{{ page.entry.featuredProjects().orderBy('sort_order', 'ASC').get }}

Because it's a relationship Pyro will compile the base models for you with the relation methods already in place.

ryanthompson  —  7 years ago

Note that I access the entry there manually since in this case the presenter won't map that method to it automatically.

william  —  7 years ago

First, thank you for your time.

{{ page.entry.featuredProjects().orderBy('sort_order', 'ASC').get }} 

Does pull the featured projects for the page. orderBy on that call does nothing (tried desc and asc and it doesnt show any difference)

Also, if i do :

{% set res = page.entry.featuredProjects().get %} or {% set res = page.entry.featuredProjects().orderBy('sort_order', 'ASC').get %} 
{% for project in res %} //.. and so on

Then i get this exception:

An exception has been thrown during the rendering of a template ("Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation") in "theme::layouts.home" at line 121.