filterBy returning the wrong URL

Working locally. I have this in my ‘product’ template:

echo $page->files()->filterBy('extension', 'pdf')

Only one PDF will be uploaded for each product, in this case. So I want to filter by the extension, and grab it. Where I’m expecting to see this:

/project/content/1-products/3-widgets/1-red-widget/file.pdf

Instead, I’m seeing the full path from my Users dir on my computer:

/Users/greg/gitrepos/project/content/1-products/3-widgets/1-red-widget/file.pdf

I’ve checked my config, and confirmed that I have this setting:

c::set('url',false);

Tried this, just to see what happened:

c::set('url', '/');

Same result.

Since you will always have one file of that type, you can safely use the findBy() and url() methods:

echo $page->documents()->findBy('extension', 'pdf')->url();

filterBy() returns a collection (of one item in this case). You could still use it but would need to add the firstmethod or use it inside a loop:

echo $page->documents()->filterBy('extension', 'pdf')->first()->url();
1 Like

Thanks Pedro, you’re a great help, as always!