Create frontend user profile with filtered project overview

Hello Community,

I want to create a user profile page with all projects the user has organised. The page-type “project” has a user field called “organiser”.

My idea was to use the filterBy-Method to filter the projects according to the current (logged in) user and display the projects in a list with a foreach-loop.

Can you tell me what I need to change to make it work?

<?php
$user = $kirby->user();
$myprojects = page('projects')->children()->filterBy('organiser', $user);
?>
    <main class="main">
        <h1><?= $page->title() ?></h1>

        <h2> Meine Projekte </h2>
            <ul>
                <?php
                foreach($myprojects as $myproject) :?> 
                    <li>
                    <a href="<?= $myproject->url() ?>">
                        <figure>
                            <?= $myproject->image()->crop(400)?>
                            <figcaption><?= $myproject->title() ?></figcaption> 
                        </figure>  
                    </a>
                    </li>
                <?php endforeach ?>
            </ul>

You have to convert the field value to a user object:

$user = $kirby->user();
$myprojects = page('projects')->children()->filter(fn ($child) => $child->organizer()->toUser() ===  $user);

If the organizer field can have multiple users:

$user = $kirby->user();
$myprojects = page('projects')->children()->filter(fn ($child) => $child->organizer()->toUsers()->has($user));

Thank you for the quick reply. Here’s the complete code (for anyone who wants to copy it):

<?php
$user = $kirby->user();
$myprojects = page('projekte')->children()->filter(fn ($child) => $child->organiser()->toUser() ===  $user);
?>
    <main class="main">
        <h1><?= $page->title() ?></h1>

        <h2> Meine Projekte </h2>

            <ul class="myprojects">
                <?php
                foreach($myprojects as $myproject) :?> 
                    <li>
                    <a href="<?= $myproject->url() ?>">
                        <figure>
                            <?= $myproject->image()->crop(400)?>
                            <figcaption><?= $myproject->title() ?></figcaption> 
                        </figure>  
                    </a>
                    </li>
                <?php endforeach ?>
            </ul>

You should make sure you really have an image, otherwise this code will throw an error if there is none:

<?php if ($image = $myproject->image()): ?>
    <figure>
        <?= $image->crop(400) ?>
        <figcaption><?= $myproject->title() ?></figcaption> 
    </figure>  
<?php endif ?>