1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Address\Repositories; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
7
|
|
|
|
8
|
|
|
abstract class EloquentBaseRepository extends RepositoryAbstract implements EloquentRepositoryInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* BaseRepository constructor. |
12
|
|
|
*/ |
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->model = $this->getModel(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get all records. |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
22
|
|
|
*/ |
23
|
|
|
public function all() |
24
|
|
|
{ |
25
|
|
|
return $this->model->newQuery()->get(); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Paginate records. |
30
|
|
|
* |
31
|
|
|
* @param int $limit |
32
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
33
|
|
|
*/ |
34
|
|
|
public function paginate($limit = 10) |
35
|
|
|
{ |
36
|
|
|
return $this->model->newQuery()->paginate($limit); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Save a new entity in repository. |
41
|
|
|
* |
42
|
|
|
* @param array $attributes |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function create(array $attributes) |
46
|
|
|
{ |
47
|
|
|
$model = $this->model->newInstance($attributes); |
48
|
|
|
$model->save(); |
49
|
|
|
|
50
|
|
|
$this->resetModel(); |
51
|
|
|
|
52
|
|
|
return $model; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model |
57
|
|
|
*/ |
58
|
|
|
public function resetModel() |
59
|
|
|
{ |
60
|
|
|
return $this->model = $this->getModel(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Make a new entity in repository. |
65
|
|
|
* |
66
|
|
|
* @param array $attributes |
67
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
68
|
|
|
*/ |
69
|
|
|
public function make(array $attributes) |
70
|
|
|
{ |
71
|
|
|
return $this->model->forceFill($attributes); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Update a entity in repository by id. |
76
|
|
|
* |
77
|
|
|
* @param array $attributes |
78
|
|
|
* @param int $id |
79
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
80
|
|
|
*/ |
81
|
|
|
public function update(array $attributes, $id) |
82
|
|
|
{ |
83
|
|
|
if ($id instanceof Model) { |
84
|
|
|
$id = $id->getKey(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$model = $this->model->findOrFail($id); |
88
|
|
|
$model->fill($attributes); |
89
|
|
|
$model->save(); |
90
|
|
|
|
91
|
|
|
$this->resetModel(); |
92
|
|
|
|
93
|
|
|
return $model; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Delete a entity in repository by id. |
98
|
|
|
* |
99
|
|
|
* @param int $id |
100
|
|
|
* @return bool|null |
101
|
|
|
* @throws \Exception |
102
|
|
|
*/ |
103
|
|
|
public function delete($id) |
104
|
|
|
{ |
105
|
|
|
if ($id instanceof Model) { |
106
|
|
|
$id = $id->getKey(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$model = $this->find($id); |
110
|
|
|
$this->resetModel(); |
111
|
|
|
|
112
|
|
|
return $model->delete(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Find data by id. |
117
|
|
|
* |
118
|
|
|
* @param int $id |
119
|
|
|
* @param array $columns |
120
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
121
|
|
|
* @throws ModelNotFoundException |
122
|
|
|
*/ |
123
|
|
|
public function find($id, $columns = ['*']) |
124
|
|
|
{ |
125
|
|
|
$model = $this->model->findOrFail($id, $columns); |
126
|
|
|
$this->resetModel(); |
127
|
|
|
|
128
|
|
|
return $model; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set repository model instance. |
133
|
|
|
* |
134
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function forModel(Model $model) |
138
|
|
|
{ |
139
|
|
|
$this->model = $model; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Count all records. |
146
|
|
|
* |
147
|
|
|
* @return int |
148
|
|
|
*/ |
149
|
|
|
public function count() |
150
|
|
|
{ |
151
|
|
|
return $this->model->count(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|