Is there a way to filter out files from a collection that have no template associated? Something like $files->hasTemplate(), returning a filtered collection?
This works:
$files->filter(function ($file) {
return !empty($file->template());
});
This works too:
$files->filterBy('template', null);
But won’t that return all files without template?
I’d say yes, that should actually do the opposite of what you want.
But I think you can shorten your code:
$files->filter(function ($file) {
return $file->template();
});
Sorry, I misunderstood your question. But it also works the way you want with:
$files->filterBy('template', true);
Or:
$files->filterBy('template', !null);
Cool! Is this documented?
I don’t think so.
I looked at the source code and files without template return null, which is a falsy value. When they have a template, the method returns a string which is a truthy value.