This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccount/app/Data/Repository/UserRepository.php

45 lines
1.1 KiB
PHP
Raw Permalink Normal View History

2019-12-02 13:26:32 +00:00
<?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;
}
}