Dealing with files field in snippets

I have a files field in my blueprint like:

fields:
    dateien:
        label: Datei
        type: files
        width: 1/2

in the corresponding snippet this works and it throws the file name(s):

<?php foreach($data->dateien() as $files): ?>
<?php foreach($files as $file): ?>
<div><?=$file?></div>
<?php endforeach ?>
<?php endforeach ?>

but this should also work, but doesn’t

<?= $data->dateien()->first()->filename() ?>

I get the error “Call to a member function first() on array”
There should be only one file, so I don’t want to loop over all of them and if there are more, I just want the first file.

Any ideas?
Thanks
Hagen

Two foreach loops don’t make sense here. You have to convert the files field to a files collection if you want a collection

$files = $data->dateien()->toFiles();
foreach ($files as $file) {
  echo $file->filename();
}

But if you want only (the first) one:

if ($file = $data->dateien()->toFiles()->first()) {
  echo $file->filename();
}

ahhhh I was missing the field method … of course the double foreach loop was a workaround, because I was not getting anything.

Thx
Hagen