getPdo(); $sql = "SELECT *, (SELECT `mail` FROM mails WHERE user_id = users.id AND `primary` = 1) AS mail FROM users;"; $sth = $pdo->prepare($sql); $sth->execute(); $res = $sth->fetchAll(\PDO::FETCH_ASSOC); $userList = []; foreach ($res as $dbEntry) { $user = new User(); $user->load($dbEntry); $userList[] = $user; } return collect($userList); } public function findById($id): ?User { $pdo = app('db')->getPdo(); $sql = "SELECT *, (SELECT `mail` FROM mails WHERE user_id = users.id AND `primary` = 1) AS mail FROM users WHERE `id` = :id;"; $sth = $pdo->prepare($sql); $sth->execute([":id" => $id]); $res = $sth->fetch(\PDO::FETCH_ASSOC); if(is_null($res)) { return null; } $user = new User(); $user->load($user); return $user; } }