1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repositories\Eloquent; |
4
|
|
|
|
5
|
|
|
use App\Repositories\Contracts\RepositoryInterface; |
6
|
|
|
use App\Repositories\Exceptions\RepositoryException; |
7
|
|
|
use Illuminate\Container\Container as App; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Repository. |
12
|
|
|
*/ |
13
|
|
|
abstract class Repository implements RepositoryInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var App |
17
|
|
|
*/ |
18
|
|
|
private $app; |
19
|
|
|
/** |
20
|
|
|
* @var |
21
|
|
|
*/ |
22
|
|
|
protected $model; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Repository constructor. |
26
|
|
|
* |
27
|
|
|
* @param App $app |
28
|
|
|
*/ |
29
|
28 |
|
public function __construct(App $app) |
30
|
|
|
{ |
31
|
28 |
|
$this->app = $app; |
32
|
28 |
|
$this->makeModel(); |
33
|
28 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
abstract public function model(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $columns |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
9 |
|
public function all($columns = ['*']) |
46
|
|
|
{ |
47
|
9 |
|
return $this->model->get($columns); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param int $perPage |
52
|
|
|
* @param null $appends |
53
|
|
|
* @param array $columns |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public function allPaginated($perPage = 15, $appends = null, $columns = ['*']) |
58
|
|
|
{ |
59
|
|
|
return $this->model->paginate($perPage, $columns)->appends($appends); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $field |
64
|
|
|
* @param $value |
65
|
|
|
* @param array $columns |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
1 |
|
public function search($field, $value, $columns = ['*']) |
70
|
|
|
{ |
71
|
1 |
|
return $this->model->where($field, 'LIKE', "%$value%")->get(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $field |
76
|
|
|
* @param $value |
77
|
|
|
* @param int $perPage |
78
|
|
|
* @param null $appends |
79
|
|
|
* @param array $columns |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function searchPaginated($field, $value, $perPage = 15, $appends = null, $columns = ['*']) |
84
|
|
|
{ |
85
|
|
|
return $this->model->where($field, 'LIKE', "%$value%")->paginate($perPage)->appends($appends); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $data |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
3 |
|
public function create(array $data) |
94
|
|
|
{ |
95
|
3 |
|
return $this->model->create($data); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $data |
100
|
|
|
* @param $id |
101
|
|
|
* @param string $attribute |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
5 |
|
public function update(array $data, $id, $attribute = 'id') |
106
|
|
|
{ |
107
|
5 |
|
return $this->model->where($attribute, '=', $id)->update($data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $data |
112
|
|
|
* @param $model |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function updateFillable(array $data, $model) |
117
|
|
|
{ |
118
|
|
|
$model->fill($data)->save(); |
119
|
|
|
|
120
|
|
|
return $model; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $id |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
4 |
|
public function delete($id) |
129
|
|
|
{ |
130
|
4 |
|
return $this->model->destroy($id); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $id |
135
|
|
|
* @param array $columns |
136
|
|
|
* |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
11 |
|
public function findOrFail($id, $columns = ['*']) |
140
|
|
|
{ |
141
|
11 |
|
return $this->model->findOrFail($id, $columns); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $attribute |
146
|
|
|
* @param $value |
147
|
|
|
* @param array $columns |
148
|
|
|
* |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
1 |
|
public function findBy($attribute, $value, $columns = ['*']) |
152
|
|
|
{ |
153
|
1 |
|
return $this->model->where($attribute, '=', $value)->get(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @throws RepositoryException |
158
|
|
|
* |
159
|
|
|
* @return Model|mixed |
160
|
|
|
*/ |
161
|
28 |
|
public function makeModel() |
162
|
|
|
{ |
163
|
28 |
|
$model = $this->app->make($this->model()); |
164
|
28 |
|
if (!$model instanceof Model) { |
165
|
|
|
throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model"); |
166
|
|
|
} |
167
|
|
|
|
168
|
28 |
|
return $this->model = $model; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|