SOLVED - Newbie - Cant get image with ($image = $special->image()->toFiles()->first()):

Hi yall!

I am very new to kirby with rudimentary php knowledge.
I am working in localhost with an as of yet unregistered version, willing to register as soon as I have my site finished for upload.

Following problem: I have profils that can create blogs. on the profil I want to display a grid with all written blogentries by that profile.
So far so good.

I collect a preview image for that reason in yml in the fields

image:
    label: Bild
    type: files
    layout: cards
    max: 1
    required: true
    uploads:
      template: image

It uploads in the folder, its there, I see a preview, but I cant get it with

<?php foreach ($specials as $special): ?>
    <?php if ($image = $special->image()->toFiles()->first()): ?>
      <img src="<?= $image->url() ?>" alt="">
    <?php endif; ?>
  <?php endforeach; ?>

I can however get it with

$image = $special-->images()->first();

However, thats not what I want.

When I try to debug it shows that the img src is empty.
Listing the images however, I can see that the images are in the folder.

I am a bit lost.
Any pointers?

I did do all the empty or delete cache things, deleted all files, images, created new ones for test purposes etc.

Thankfull for any help.

Oh - in the blueprint the Image is saved like so:

----

Image: - file://7yhhih5nt7emyyqg

----

And in the jpg.txt like so:

Uuid: 7yhhih5nt7emyyqg

----

Template: image

I also do have a image.yml in /blueprints/files looking like so:

title: Bild

accept:
  type: image
  mime:
    - image/jpeg
    - image/png
    - image/gif
    - image/webp

Solved.
I am an idiot, needed to use POP (Power of Posting) and learned RIGHT AFTER that you cant call a field image as this is a special kirby name.
I renamed it to preview image and now it works.

Did only cost me 2 hours - ahhhhhhhhhhh!

May it save others as a reminder.

1 Like

If you have configured the field with max: 1 in your blueprint, the selection is already limited to a single image. In this case, you do not need a foreach loop or ->first(), as you can access the image directly with ->toFile().

It also makes sense to use an if query to check whether an image actually exists before accessing it – this saves you unnecessary processing if the field is empty.

<?php if ($image = $page->image()->toFile()): ?>
  <img src="<?= $image->url() ?>" alt="<?= $image->alt()->esc() ?>">
<?php endif; ?>