-
-
Notifications
You must be signed in to change notification settings - Fork 636
Expand file tree
/
Copy pathAccountRepository.php
More file actions
80 lines (69 loc) · 2.47 KB
/
AccountRepository.php
File metadata and controls
80 lines (69 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace HiEvents\Repository\Eloquent;
use HiEvents\DomainObjects\AccountDomainObject;
use HiEvents\Models\Account;
use HiEvents\Repository\Interfaces\AccountRepositoryInterface;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<AccountDomainObject>
*/
class AccountRepository extends BaseRepository implements AccountRepositoryInterface
{
protected function getModel(): string
{
return Account::class;
}
public function getDomainObject(): string
{
return AccountDomainObject::class;
}
public function findByEventId(int $eventId): AccountDomainObject
{
$account = $this
->model
->select('accounts.*')
->join('events', 'accounts.id', '=', 'events.account_id')
->where('events.id', $eventId)
->first();
return $this->handleSingleResult($account, AccountDomainObject::class);
}
public function getAllAccountsWithCounts(?string $search, int $perPage): LengthAwarePaginator
{
$query = $this->model
->select('accounts.*')
->withCount(['events', 'users'])
->with([
'users' => function ($query) {
$query->select('users.id', 'users.first_name', 'users.last_name', 'users.email')
->withPivot('role');
},
'messagingTier',
]);
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('accounts.name', 'like', "{$search}%")
->orWhere('accounts.email', 'like', "{$search}%")
->orWhereHas('users', function ($userQuery) use ($search) {
$userQuery->where('users.email', 'like', "{$search}%");
});
});
}
return $query->orderBy('created_at', 'desc')->paginate($perPage);
}
public function getAccountWithDetails(int $accountId): Account
{
return $this->model
->withCount(['events', 'users'])
->with([
'configuration',
'account_vat_setting',
'messagingTier',
'users' => function ($query) {
$query->select('users.id', 'users.first_name', 'users.last_name', 'users.email')
->withPivot('role');
}
])
->findOrFail($accountId);
}
}