Skip to content

Commit 629355a

Browse files
committed
Base Repository (basic)
1 parent 719b77a commit 629355a

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace TeachMe\Repositories;
4+
5+
abstract class BaseRepository {
6+
7+
/**
8+
* @return \TeachMe\Entities\Entity
9+
*/
10+
abstract public function getModel();
11+
12+
/**
13+
* @return \Illuminate\Database\Eloquent\Builder
14+
*/
15+
public function newQuery()
16+
{
17+
return $this->getModel()->newQuery();
18+
}
19+
20+
public function findOrFail($id)
21+
{
22+
return $this->newQuery()->findOrFail($id);
23+
}
24+
25+
}

app/Repositories/TicketRepository.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use TeachMe\Entities\Ticket;
66

7-
class TicketRepository {
7+
class TicketRepository extends BaseRepository {
88

99
public function getModel()
1010
{
@@ -13,7 +13,7 @@ public function getModel()
1313

1414
protected function selectTicketsList()
1515
{
16-
return Ticket::selectRaw(
16+
return $this->newQuery()->selectRaw(
1717
'tickets.*, '
1818
. '( SELECT COUNT(*) FROM ticket_comments WHERE ticket_comments.ticket_id = tickets.id ) as num_comments,'
1919
. '( SELECT COUNT(*) FROM ticket_votes WHERE ticket_votes.ticket_id = tickets.id ) as num_votes'
@@ -43,9 +43,4 @@ public function paginateClosed()
4343
->paginate(20);
4444
}
4545

46-
public function findOrFail($id)
47-
{
48-
return Ticket::findOrFail($id);
49-
}
50-
5146
}

0 commit comments

Comments
 (0)