Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Youtaka's avatar

Get custom relationship data with Eloquent query builder

I have this query :

FHINotification::with(['log'])
    ->where('user', $user->getSAMAccountName())
    ->whereNull('read_at')
    ->get();

"Log" model has a function to get custom data (more human readable than data in db)

public function getData(): array
{
    Game::setWithoutStandardEdition(true);
    $ldapService = App::make('App\Services\LdapService');
    $log['url'] = $this->getUrl($this->task->resourceable_type, $this->task->string_resourceable_id);
    $log['action'] = $this->getActionName($this->action);
    $log['resource_name'] = $this->getResourceName($this->task->resourceable);
    $log['user'] = $ldapService->getUserByUsername($this->user)->getDisplayName();

    return $log;
}

I can call this method from a Log instance, for example: $log->getData() but is it possible to call it from a query builder ? If not, I have to loop on my query result to format each one of them.

Thank you :)

0 likes
2 replies
Youtaka's avatar

This is what I want, perfect, thank you =)

Please or to participate in this conversation.