keksAccount/app/Data/Repository/UserRepository.php

45 lines
1.1 KiB
PHP

<?php
namespace App\Data\Repository;
use App\Data\Entity\User;
class UserRepository {
public function getAllUsers() {
$pdo = app('db')->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;
}
}