-
Notifications
You must be signed in to change notification settings - Fork 8
/
Repository.php
170 lines (152 loc) · 3.42 KB
/
Repository.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace App\Repositories\Eloquent;
use App\Repositories\Contracts\RepositoryInterface;
use App\Repositories\Exceptions\RepositoryException;
use Illuminate\Container\Container as App;
use Illuminate\Database\Eloquent\Model;
/**
* Class Repository.
*/
abstract class Repository implements RepositoryInterface
{
/**
* @var App
*/
private $app;
/**
* @var
*/
protected $model;
/**
* Repository constructor.
*
* @param App $app
*/
public function __construct(App $app)
{
$this->app = $app;
$this->makeModel();
}
/**
* @return mixed
*/
abstract public function model();
/**
* @param array $columns
*
* @return mixed
*/
public function all($columns = ['*'])
{
return $this->model->get($columns);
}
/**
* @param int $perPage
* @param null $appends
* @param array $columns
*
* @return mixed
*/
public function allPaginated($perPage = 15, $appends = null, $columns = ['*'])
{
return $this->model->paginate($perPage, $columns)->appends($appends);
}
/**
* @param $field
* @param $value
* @param array $columns
*
* @return mixed
*/
public function search($field, $value, $columns = ['*'])
{
return $this->model->where($field, 'LIKE', "%$value%")->get();
}
/**
* @param $field
* @param $value
* @param int $perPage
* @param null $appends
* @param array $columns
*
* @return mixed
*/
public function searchPaginated($field, $value, $perPage = 15, $appends = null, $columns = ['*'])
{
return $this->model->where($field, 'LIKE', "%$value%")->paginate($perPage)->appends($appends);
}
/**
* @param array $data
*
* @return mixed
*/
public function create(array $data)
{
return $this->model->create($data);
}
/**
* @param array $data
* @param $id
* @param string $attribute
*
* @return mixed
*/
public function update(array $data, $id, $attribute = 'id')
{
return $this->model->where($attribute, '=', $id)->update($data);
}
/**
* @param array $data
* @param $model
*
* @return mixed
*/
public function updateFillable(array $data, $model)
{
$model->fill($data)->save();
return $model;
}
/**
* @param $id
*
* @return mixed
*/
public function delete($id)
{
return $this->model->destroy($id);
}
/**
* @param $id
* @param array $columns
*
* @return mixed
*/
public function findOrFail($id, $columns = ['*'])
{
return $this->model->findOrFail($id, $columns);
}
/**
* @param $attribute
* @param $value
* @param array $columns
*
* @return mixed
*/
public function findBy($attribute, $value, $columns = ['*'])
{
return $this->model->where($attribute, '=', $value)->get();
}
/**
* @throws RepositoryException
*
* @return Model|mixed
*/
public function makeModel()
{
$model = $this->app->make($this->model());
if (!$model instanceof Model) {
throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
}
return $this->model = $model;
}
}