@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @param array $relations |
29 | 29 | * @param string $sortBy |
30 | - * @param boolean $desc |
|
30 | + * @param integer $desc |
|
31 | 31 | * @param array $columns |
32 | 32 | * @return collection |
33 | 33 | */ |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * @param integer $perPage |
44 | 44 | * @param array $relations |
45 | 45 | * @param string $sortBy |
46 | - * @param boolean $desc |
|
46 | + * @param integer $desc |
|
47 | 47 | * @param array $columns |
48 | 48 | * @return collection |
49 | 49 | */ |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * @param integer $perPage |
62 | 62 | * @param array $relations |
63 | 63 | * @param string $sortBy |
64 | - * @param boolean $desc |
|
64 | + * @param integer $desc |
|
65 | 65 | * @param array $columns |
66 | 66 | * @return collection |
67 | 67 | */ |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * Save the given model to the storage. |
77 | 77 | * |
78 | 78 | * @param array $data |
79 | - * @return mixed |
|
79 | + * @return boolean |
|
80 | 80 | */ |
81 | 81 | public function save(array $data) |
82 | 82 | { |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | * @param array $conditions array of conditions |
133 | 133 | * @param array $relations |
134 | 134 | * @param string $sortBy |
135 | - * @param boolean $desc |
|
135 | + * @param integer $desc |
|
136 | 136 | * @param array $columns |
137 | 137 | * @return collection |
138 | 138 | */ |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | * @param array $conditions array of conditions |
165 | 165 | * @param integer $perPage |
166 | 166 | * @param string $sortBy |
167 | - * @param boolean $desc |
|
167 | + * @param integer $desc |
|
168 | 168 | * @param array $columns |
169 | 169 | * @return collection |
170 | 170 | */ |
@@ -6,537 +6,537 @@ |
||
6 | 6 | |
7 | 7 | abstract class BaseRepository implements BaseRepositoryInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * @var object |
|
11 | - */ |
|
12 | - public $model; |
|
9 | + /** |
|
10 | + * @var object |
|
11 | + */ |
|
12 | + public $model; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Init new object. |
|
16 | - * |
|
17 | - * @var mixed model |
|
18 | - * @return void |
|
19 | - */ |
|
20 | - public function __construct($model) |
|
21 | - { |
|
22 | - $this->model = $model; |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * Fetch all records with relations from the storage. |
|
27 | - * |
|
28 | - * @param array $relations |
|
29 | - * @param string $sortBy |
|
30 | - * @param boolean $desc |
|
31 | - * @param array $columns |
|
32 | - * @return collection |
|
33 | - */ |
|
34 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
35 | - { |
|
36 | - $sort = $desc ? 'desc' : 'asc'; |
|
37 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
38 | - } |
|
14 | + /** |
|
15 | + * Init new object. |
|
16 | + * |
|
17 | + * @var mixed model |
|
18 | + * @return void |
|
19 | + */ |
|
20 | + public function __construct($model) |
|
21 | + { |
|
22 | + $this->model = $model; |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * Fetch all records with relations from the storage. |
|
27 | + * |
|
28 | + * @param array $relations |
|
29 | + * @param string $sortBy |
|
30 | + * @param boolean $desc |
|
31 | + * @param array $columns |
|
32 | + * @return collection |
|
33 | + */ |
|
34 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
35 | + { |
|
36 | + $sort = $desc ? 'desc' : 'asc'; |
|
37 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Fetch all records with relations from storage in pages. |
|
42 | - * |
|
43 | - * @param integer $perPage |
|
44 | - * @param array $relations |
|
45 | - * @param string $sortBy |
|
46 | - * @param boolean $desc |
|
47 | - * @param array $columns |
|
48 | - * @return collection |
|
49 | - */ |
|
50 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
51 | - { |
|
52 | - $sort = $desc ? 'desc' : 'asc'; |
|
53 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Fetch all records with relations based on |
|
58 | - * the given condition from storage in pages. |
|
59 | - * |
|
60 | - * @param array $conditions array of conditions |
|
61 | - * @param integer $perPage |
|
62 | - * @param array $relations |
|
63 | - * @param string $sortBy |
|
64 | - * @param boolean $desc |
|
65 | - * @param array $columns |
|
66 | - * @return collection |
|
67 | - */ |
|
68 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
69 | - { |
|
70 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
71 | - $sort = $desc ? 'desc' : 'asc'; |
|
72 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
73 | - } |
|
40 | + /** |
|
41 | + * Fetch all records with relations from storage in pages. |
|
42 | + * |
|
43 | + * @param integer $perPage |
|
44 | + * @param array $relations |
|
45 | + * @param string $sortBy |
|
46 | + * @param boolean $desc |
|
47 | + * @param array $columns |
|
48 | + * @return collection |
|
49 | + */ |
|
50 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
51 | + { |
|
52 | + $sort = $desc ? 'desc' : 'asc'; |
|
53 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Fetch all records with relations based on |
|
58 | + * the given condition from storage in pages. |
|
59 | + * |
|
60 | + * @param array $conditions array of conditions |
|
61 | + * @param integer $perPage |
|
62 | + * @param array $relations |
|
63 | + * @param string $sortBy |
|
64 | + * @param boolean $desc |
|
65 | + * @param array $columns |
|
66 | + * @return collection |
|
67 | + */ |
|
68 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
69 | + { |
|
70 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
71 | + $sort = $desc ? 'desc' : 'asc'; |
|
72 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Save the given model to the storage. |
|
77 | - * |
|
78 | - * @param array $data |
|
79 | - * @return mixed |
|
80 | - */ |
|
81 | - public function save(array $data) |
|
82 | - { |
|
83 | - \Session::put('locale', 'all'); |
|
84 | - $model = false; |
|
85 | - $relations = []; |
|
86 | - |
|
87 | - \DB::transaction(function () use (&$model, $relations, $data) { |
|
75 | + /** |
|
76 | + * Save the given model to the storage. |
|
77 | + * |
|
78 | + * @param array $data |
|
79 | + * @return mixed |
|
80 | + */ |
|
81 | + public function save(array $data) |
|
82 | + { |
|
83 | + \Session::put('locale', 'all'); |
|
84 | + $model = false; |
|
85 | + $relations = []; |
|
86 | + |
|
87 | + \DB::transaction(function () use (&$model, $relations, $data) { |
|
88 | 88 | |
89 | - $model = $this->prepareModel($data); |
|
90 | - $relations = $this->prepareRelations($data, $model); |
|
91 | - $model = $this->saveModel($model, $relations); |
|
92 | - }); |
|
89 | + $model = $this->prepareModel($data); |
|
90 | + $relations = $this->prepareRelations($data, $model); |
|
91 | + $model = $this->saveModel($model, $relations); |
|
92 | + }); |
|
93 | 93 | |
94 | - return $model; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Delete record from the storage based on the given |
|
99 | - * condition. |
|
100 | - * |
|
101 | - * @param var $value condition value |
|
102 | - * @param string $attribute condition column name |
|
103 | - * @return void |
|
104 | - */ |
|
105 | - public function delete($value, $attribute = 'id') |
|
106 | - { |
|
107 | - \DB::transaction(function () use ($value, $attribute) { |
|
108 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
109 | - $model->delete(); |
|
110 | - }); |
|
111 | - }); |
|
112 | - } |
|
94 | + return $model; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Delete record from the storage based on the given |
|
99 | + * condition. |
|
100 | + * |
|
101 | + * @param var $value condition value |
|
102 | + * @param string $attribute condition column name |
|
103 | + * @return void |
|
104 | + */ |
|
105 | + public function delete($value, $attribute = 'id') |
|
106 | + { |
|
107 | + \DB::transaction(function () use ($value, $attribute) { |
|
108 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
109 | + $model->delete(); |
|
110 | + }); |
|
111 | + }); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Fetch records from the storage based on the given |
|
116 | - * id. |
|
117 | - * |
|
118 | - * @param integer $id |
|
119 | - * @param string[] $relations |
|
120 | - * @param array $columns |
|
121 | - * @return object |
|
122 | - */ |
|
123 | - public function find($id, $relations = [], $columns = ['*']) |
|
124 | - { |
|
125 | - return $this->model->with($relations)->find($id, $columns); |
|
126 | - } |
|
114 | + /** |
|
115 | + * Fetch records from the storage based on the given |
|
116 | + * id. |
|
117 | + * |
|
118 | + * @param integer $id |
|
119 | + * @param string[] $relations |
|
120 | + * @param array $columns |
|
121 | + * @return object |
|
122 | + */ |
|
123 | + public function find($id, $relations = [], $columns = ['*']) |
|
124 | + { |
|
125 | + return $this->model->with($relations)->find($id, $columns); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Fetch records from the storage based on the given |
|
130 | - * condition. |
|
131 | - * |
|
132 | - * @param array $conditions array of conditions |
|
133 | - * @param array $relations |
|
134 | - * @param string $sortBy |
|
135 | - * @param boolean $desc |
|
136 | - * @param array $columns |
|
137 | - * @return collection |
|
138 | - */ |
|
139 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
140 | - { |
|
141 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
142 | - $sort = $desc ? 'desc' : 'asc'; |
|
143 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Fetch the first record from the storage based on the given |
|
148 | - * condition. |
|
149 | - * |
|
150 | - * @param array $conditions array of conditions |
|
151 | - * @param array $relations |
|
152 | - * @param array $columns |
|
153 | - * @return object |
|
154 | - */ |
|
155 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
156 | - { |
|
157 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
158 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Return the deleted models in pages based on the given conditions. |
|
163 | - * |
|
164 | - * @param array $conditions array of conditions |
|
165 | - * @param integer $perPage |
|
166 | - * @param string $sortBy |
|
167 | - * @param boolean $desc |
|
168 | - * @param array $columns |
|
169 | - * @return collection |
|
170 | - */ |
|
171 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
172 | - { |
|
173 | - unset($conditions['page']); |
|
174 | - unset($conditions['perPage']); |
|
175 | - unset($conditions['sortBy']); |
|
176 | - unset($conditions['sort']); |
|
177 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
178 | - $sort = $desc ? 'desc' : 'asc'; |
|
179 | - $model = $this->model->onlyTrashed(); |
|
180 | - |
|
181 | - if (count($conditions['conditionValues'])) { |
|
182 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
183 | - } |
|
184 | - |
|
185 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Restore the deleted model. |
|
190 | - * |
|
191 | - * @param integer $id |
|
192 | - * @return void |
|
193 | - */ |
|
194 | - public function restore($id) |
|
195 | - { |
|
196 | - $model = $this->model->onlyTrashed()->find($id); |
|
197 | - |
|
198 | - if (! $model) { |
|
199 | - \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
200 | - } |
|
201 | - |
|
202 | - $model->restore(); |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Fill the model with the given data. |
|
207 | - * |
|
208 | - * @param array $data |
|
209 | - * |
|
210 | - * @return object |
|
211 | - */ |
|
212 | - public function prepareModel($data) |
|
213 | - { |
|
214 | - $modelClass = $this->model; |
|
215 | - |
|
216 | - /** |
|
217 | - * If the id is present in the data then select the model for updating, |
|
218 | - * else create new model. |
|
219 | - * @var array |
|
220 | - */ |
|
221 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
222 | - if (! $model) { |
|
223 | - \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Construct the model object with the given data, |
|
228 | - * and if there is a relation add it to relations array, |
|
229 | - * then save the model. |
|
230 | - */ |
|
231 | - foreach ($data as $key => $value) { |
|
232 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
233 | - /** |
|
234 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
235 | - */ |
|
236 | - $model->$key = $value; |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - return $model; |
|
241 | - } |
|
128 | + /** |
|
129 | + * Fetch records from the storage based on the given |
|
130 | + * condition. |
|
131 | + * |
|
132 | + * @param array $conditions array of conditions |
|
133 | + * @param array $relations |
|
134 | + * @param string $sortBy |
|
135 | + * @param boolean $desc |
|
136 | + * @param array $columns |
|
137 | + * @return collection |
|
138 | + */ |
|
139 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
140 | + { |
|
141 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
142 | + $sort = $desc ? 'desc' : 'asc'; |
|
143 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Fetch the first record from the storage based on the given |
|
148 | + * condition. |
|
149 | + * |
|
150 | + * @param array $conditions array of conditions |
|
151 | + * @param array $relations |
|
152 | + * @param array $columns |
|
153 | + * @return object |
|
154 | + */ |
|
155 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
156 | + { |
|
157 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
158 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Return the deleted models in pages based on the given conditions. |
|
163 | + * |
|
164 | + * @param array $conditions array of conditions |
|
165 | + * @param integer $perPage |
|
166 | + * @param string $sortBy |
|
167 | + * @param boolean $desc |
|
168 | + * @param array $columns |
|
169 | + * @return collection |
|
170 | + */ |
|
171 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
172 | + { |
|
173 | + unset($conditions['page']); |
|
174 | + unset($conditions['perPage']); |
|
175 | + unset($conditions['sortBy']); |
|
176 | + unset($conditions['sort']); |
|
177 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
178 | + $sort = $desc ? 'desc' : 'asc'; |
|
179 | + $model = $this->model->onlyTrashed(); |
|
180 | + |
|
181 | + if (count($conditions['conditionValues'])) { |
|
182 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
183 | + } |
|
184 | + |
|
185 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Restore the deleted model. |
|
190 | + * |
|
191 | + * @param integer $id |
|
192 | + * @return void |
|
193 | + */ |
|
194 | + public function restore($id) |
|
195 | + { |
|
196 | + $model = $this->model->onlyTrashed()->find($id); |
|
197 | + |
|
198 | + if (! $model) { |
|
199 | + \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
200 | + } |
|
201 | + |
|
202 | + $model->restore(); |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Fill the model with the given data. |
|
207 | + * |
|
208 | + * @param array $data |
|
209 | + * |
|
210 | + * @return object |
|
211 | + */ |
|
212 | + public function prepareModel($data) |
|
213 | + { |
|
214 | + $modelClass = $this->model; |
|
215 | + |
|
216 | + /** |
|
217 | + * If the id is present in the data then select the model for updating, |
|
218 | + * else create new model. |
|
219 | + * @var array |
|
220 | + */ |
|
221 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
222 | + if (! $model) { |
|
223 | + \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Construct the model object with the given data, |
|
228 | + * and if there is a relation add it to relations array, |
|
229 | + * then save the model. |
|
230 | + */ |
|
231 | + foreach ($data as $key => $value) { |
|
232 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
233 | + /** |
|
234 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
235 | + */ |
|
236 | + $model->$key = $value; |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + return $model; |
|
241 | + } |
|
242 | 242 | |
243 | - /** |
|
244 | - * Prepare related models based on the given data for the given model. |
|
245 | - * |
|
246 | - * @param array $data |
|
247 | - * @param object $model |
|
248 | - * |
|
249 | - * @return array |
|
250 | - */ |
|
251 | - public function prepareRelations($data, $model) |
|
252 | - { |
|
253 | - /** |
|
254 | - * Construct the model object with the given data, |
|
255 | - * and if there is a relation add it to relations array, |
|
256 | - * then save the model. |
|
257 | - */ |
|
258 | - foreach ($data as $key => $value) { |
|
259 | - /** |
|
260 | - * If the attribute is a relation. |
|
261 | - */ |
|
262 | - $relation = \Str::camel($key); |
|
263 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
264 | - /** |
|
265 | - * Check if the relation is a collection. |
|
266 | - */ |
|
267 | - if (class_basename($model->$relation) == 'Collection') { |
|
268 | - /** |
|
269 | - * If the relation has no value then marke the relation data |
|
270 | - * related to the model to be deleted. |
|
271 | - */ |
|
272 | - if (! $value || ! count($value)) { |
|
273 | - $relations[$relation] = 'delete'; |
|
274 | - } |
|
275 | - } |
|
276 | - if (is_array($value)) { |
|
277 | - /** |
|
278 | - * Loop through the relation data. |
|
279 | - */ |
|
280 | - foreach ($value as $attr => $val) { |
|
281 | - /** |
|
282 | - * Get the relation model. |
|
283 | - */ |
|
284 | - $relationBaseModel = \Core::$relation()->model; |
|
285 | - |
|
286 | - /** |
|
287 | - * Check if the relation is a collection. |
|
288 | - */ |
|
289 | - if (class_basename($model->$relation) == 'Collection') { |
|
290 | - /** |
|
291 | - * If the id is present in the data then select the relation model for updating, |
|
292 | - * else create new model. |
|
293 | - */ |
|
294 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
295 | - |
|
296 | - /** |
|
297 | - * If model doesn't exists. |
|
298 | - */ |
|
299 | - if (! $relationModel) { |
|
300 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Loop through the relation attributes. |
|
305 | - */ |
|
306 | - foreach ($val as $attr => $val) { |
|
307 | - /** |
|
308 | - * Prevent the sub relations or attributes not in the fillable. |
|
309 | - */ |
|
310 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
311 | - $relationModel->$attr = $val; |
|
312 | - } |
|
313 | - } |
|
314 | - |
|
315 | - $relations[$relation][] = $relationModel; |
|
316 | - } else { |
|
317 | - /** |
|
318 | - * Prevent the sub relations. |
|
319 | - */ |
|
320 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
321 | - /** |
|
322 | - * If the id is present in the data then select the relation model for updating, |
|
323 | - * else create new model. |
|
324 | - */ |
|
325 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
326 | - |
|
327 | - /** |
|
328 | - * If model doesn't exists. |
|
329 | - */ |
|
330 | - if (! $relationModel) { |
|
331 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
332 | - } |
|
333 | - |
|
334 | - foreach ($value as $relationAttribute => $relationValue) { |
|
335 | - /** |
|
336 | - * Prevent attributes not in the fillable. |
|
337 | - */ |
|
338 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
339 | - $relationModel->$relationAttribute = $relationValue; |
|
340 | - } |
|
341 | - } |
|
342 | - |
|
343 | - $relations[$relation] = $relationModel; |
|
344 | - } |
|
345 | - } |
|
346 | - } |
|
347 | - } |
|
348 | - } |
|
349 | - } |
|
350 | - |
|
351 | - return $relations; |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Save the model with related models. |
|
356 | - * |
|
357 | - * @param object $model |
|
358 | - * @param array $relations |
|
359 | - * |
|
360 | - * @return object |
|
361 | - */ |
|
362 | - public function saveModel($model, $relations) |
|
363 | - { |
|
364 | - |
|
365 | - /** |
|
366 | - * Loop through the relations array. |
|
367 | - */ |
|
368 | - foreach ($relations as $key => $value) { |
|
369 | - /** |
|
370 | - * If the relation is marked for delete then delete it. |
|
371 | - */ |
|
372 | - if ($value == 'delete' && $model->$key()->count()) { |
|
373 | - $model->$key()->delete(); |
|
374 | - } elseif (gettype($value) == 'array') { |
|
375 | - /** |
|
376 | - * Save the model. |
|
377 | - */ |
|
378 | - $model->save(); |
|
379 | - $ids = []; |
|
380 | - |
|
381 | - /** |
|
382 | - * Loop through the relations. |
|
383 | - */ |
|
384 | - foreach ($value as $val) { |
|
385 | - switch (class_basename($model->$key())) { |
|
386 | - /** |
|
387 | - * If the relation is one to many then update it's foreign key with |
|
388 | - * the model id and save it then add its id to ids array to delete all |
|
389 | - * relations who's id isn't in the ids array. |
|
390 | - */ |
|
391 | - case 'HasMany': |
|
392 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
393 | - $val->$foreignKeyName = $model->id; |
|
394 | - $val->save(); |
|
395 | - $ids[] = $val->id; |
|
396 | - break; |
|
397 | - |
|
398 | - /** |
|
399 | - * If the relation is many to many then add it's id to the ids array to |
|
400 | - * attache these ids to the model. |
|
401 | - */ |
|
402 | - case 'BelongsToMany': |
|
403 | - $val->save(); |
|
404 | - $ids[] = $val->id; |
|
405 | - break; |
|
406 | - } |
|
407 | - } |
|
408 | - switch (class_basename($model->$key())) { |
|
409 | - /** |
|
410 | - * If the relation is one to many then delete all |
|
411 | - * relations who's id isn't in the ids array. |
|
412 | - */ |
|
413 | - case 'HasMany': |
|
414 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
415 | - break; |
|
416 | - |
|
417 | - /** |
|
418 | - * If the relation is many to many then |
|
419 | - * detach the previous data and attach |
|
420 | - * the ids array to the model. |
|
421 | - */ |
|
422 | - case 'BelongsToMany': |
|
423 | - $model->$key()->detach(); |
|
424 | - $model->$key()->attach($ids); |
|
425 | - break; |
|
426 | - } |
|
427 | - } else { |
|
428 | - switch (class_basename($model->$key())) { |
|
429 | - /** |
|
430 | - * If the relation is one to one. |
|
431 | - */ |
|
432 | - case 'HasOne': |
|
433 | - /** |
|
434 | - * Save the model. |
|
435 | - */ |
|
436 | - $model->save(); |
|
437 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
438 | - $value->$foreignKeyName = $model->id; |
|
439 | - $value->save(); |
|
440 | - break; |
|
441 | - case 'BelongsTo': |
|
442 | - /** |
|
443 | - * Save the model. |
|
444 | - */ |
|
445 | - $value->save(); |
|
446 | - $model->$key()->associate($value); |
|
447 | - break; |
|
448 | - } |
|
449 | - } |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Save the model. |
|
454 | - */ |
|
455 | - $model->save(); |
|
456 | - |
|
457 | - return $model; |
|
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * Build the conditions recursively for the retrieving methods. |
|
462 | - * @param array $conditions |
|
463 | - * @return array |
|
464 | - */ |
|
465 | - protected function constructConditions($conditions, $model) |
|
466 | - { |
|
467 | - $conditionString = ''; |
|
468 | - $conditionValues = []; |
|
469 | - foreach ($conditions as $key => $value) { |
|
470 | - if (Str::contains($key, '->')) { |
|
471 | - $key = $this->wrapJsonSelector($key); |
|
472 | - } |
|
473 | - |
|
474 | - if ($key == 'and') { |
|
475 | - $conditions = $this->constructConditions($value, $model); |
|
476 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
477 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
478 | - } elseif ($key == 'or') { |
|
479 | - $conditions = $this->constructConditions($value, $model); |
|
480 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
481 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
482 | - } else { |
|
483 | - if (is_array($value)) { |
|
484 | - $operator = $value['op']; |
|
485 | - if (strtolower($operator) == 'between') { |
|
486 | - $value1 = $value['val1']; |
|
487 | - $value2 = $value['val2']; |
|
488 | - } else { |
|
489 | - $value = Arr::get($value, 'val', ''); |
|
490 | - } |
|
491 | - } else { |
|
492 | - $operator = '='; |
|
493 | - } |
|
243 | + /** |
|
244 | + * Prepare related models based on the given data for the given model. |
|
245 | + * |
|
246 | + * @param array $data |
|
247 | + * @param object $model |
|
248 | + * |
|
249 | + * @return array |
|
250 | + */ |
|
251 | + public function prepareRelations($data, $model) |
|
252 | + { |
|
253 | + /** |
|
254 | + * Construct the model object with the given data, |
|
255 | + * and if there is a relation add it to relations array, |
|
256 | + * then save the model. |
|
257 | + */ |
|
258 | + foreach ($data as $key => $value) { |
|
259 | + /** |
|
260 | + * If the attribute is a relation. |
|
261 | + */ |
|
262 | + $relation = \Str::camel($key); |
|
263 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
264 | + /** |
|
265 | + * Check if the relation is a collection. |
|
266 | + */ |
|
267 | + if (class_basename($model->$relation) == 'Collection') { |
|
268 | + /** |
|
269 | + * If the relation has no value then marke the relation data |
|
270 | + * related to the model to be deleted. |
|
271 | + */ |
|
272 | + if (! $value || ! count($value)) { |
|
273 | + $relations[$relation] = 'delete'; |
|
274 | + } |
|
275 | + } |
|
276 | + if (is_array($value)) { |
|
277 | + /** |
|
278 | + * Loop through the relation data. |
|
279 | + */ |
|
280 | + foreach ($value as $attr => $val) { |
|
281 | + /** |
|
282 | + * Get the relation model. |
|
283 | + */ |
|
284 | + $relationBaseModel = \Core::$relation()->model; |
|
285 | + |
|
286 | + /** |
|
287 | + * Check if the relation is a collection. |
|
288 | + */ |
|
289 | + if (class_basename($model->$relation) == 'Collection') { |
|
290 | + /** |
|
291 | + * If the id is present in the data then select the relation model for updating, |
|
292 | + * else create new model. |
|
293 | + */ |
|
294 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
295 | + |
|
296 | + /** |
|
297 | + * If model doesn't exists. |
|
298 | + */ |
|
299 | + if (! $relationModel) { |
|
300 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Loop through the relation attributes. |
|
305 | + */ |
|
306 | + foreach ($val as $attr => $val) { |
|
307 | + /** |
|
308 | + * Prevent the sub relations or attributes not in the fillable. |
|
309 | + */ |
|
310 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
311 | + $relationModel->$attr = $val; |
|
312 | + } |
|
313 | + } |
|
314 | + |
|
315 | + $relations[$relation][] = $relationModel; |
|
316 | + } else { |
|
317 | + /** |
|
318 | + * Prevent the sub relations. |
|
319 | + */ |
|
320 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
321 | + /** |
|
322 | + * If the id is present in the data then select the relation model for updating, |
|
323 | + * else create new model. |
|
324 | + */ |
|
325 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
326 | + |
|
327 | + /** |
|
328 | + * If model doesn't exists. |
|
329 | + */ |
|
330 | + if (! $relationModel) { |
|
331 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
332 | + } |
|
333 | + |
|
334 | + foreach ($value as $relationAttribute => $relationValue) { |
|
335 | + /** |
|
336 | + * Prevent attributes not in the fillable. |
|
337 | + */ |
|
338 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
339 | + $relationModel->$relationAttribute = $relationValue; |
|
340 | + } |
|
341 | + } |
|
342 | + |
|
343 | + $relations[$relation] = $relationModel; |
|
344 | + } |
|
345 | + } |
|
346 | + } |
|
347 | + } |
|
348 | + } |
|
349 | + } |
|
350 | + |
|
351 | + return $relations; |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Save the model with related models. |
|
356 | + * |
|
357 | + * @param object $model |
|
358 | + * @param array $relations |
|
359 | + * |
|
360 | + * @return object |
|
361 | + */ |
|
362 | + public function saveModel($model, $relations) |
|
363 | + { |
|
364 | + |
|
365 | + /** |
|
366 | + * Loop through the relations array. |
|
367 | + */ |
|
368 | + foreach ($relations as $key => $value) { |
|
369 | + /** |
|
370 | + * If the relation is marked for delete then delete it. |
|
371 | + */ |
|
372 | + if ($value == 'delete' && $model->$key()->count()) { |
|
373 | + $model->$key()->delete(); |
|
374 | + } elseif (gettype($value) == 'array') { |
|
375 | + /** |
|
376 | + * Save the model. |
|
377 | + */ |
|
378 | + $model->save(); |
|
379 | + $ids = []; |
|
380 | + |
|
381 | + /** |
|
382 | + * Loop through the relations. |
|
383 | + */ |
|
384 | + foreach ($value as $val) { |
|
385 | + switch (class_basename($model->$key())) { |
|
386 | + /** |
|
387 | + * If the relation is one to many then update it's foreign key with |
|
388 | + * the model id and save it then add its id to ids array to delete all |
|
389 | + * relations who's id isn't in the ids array. |
|
390 | + */ |
|
391 | + case 'HasMany': |
|
392 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
393 | + $val->$foreignKeyName = $model->id; |
|
394 | + $val->save(); |
|
395 | + $ids[] = $val->id; |
|
396 | + break; |
|
397 | + |
|
398 | + /** |
|
399 | + * If the relation is many to many then add it's id to the ids array to |
|
400 | + * attache these ids to the model. |
|
401 | + */ |
|
402 | + case 'BelongsToMany': |
|
403 | + $val->save(); |
|
404 | + $ids[] = $val->id; |
|
405 | + break; |
|
406 | + } |
|
407 | + } |
|
408 | + switch (class_basename($model->$key())) { |
|
409 | + /** |
|
410 | + * If the relation is one to many then delete all |
|
411 | + * relations who's id isn't in the ids array. |
|
412 | + */ |
|
413 | + case 'HasMany': |
|
414 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
415 | + break; |
|
416 | + |
|
417 | + /** |
|
418 | + * If the relation is many to many then |
|
419 | + * detach the previous data and attach |
|
420 | + * the ids array to the model. |
|
421 | + */ |
|
422 | + case 'BelongsToMany': |
|
423 | + $model->$key()->detach(); |
|
424 | + $model->$key()->attach($ids); |
|
425 | + break; |
|
426 | + } |
|
427 | + } else { |
|
428 | + switch (class_basename($model->$key())) { |
|
429 | + /** |
|
430 | + * If the relation is one to one. |
|
431 | + */ |
|
432 | + case 'HasOne': |
|
433 | + /** |
|
434 | + * Save the model. |
|
435 | + */ |
|
436 | + $model->save(); |
|
437 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
438 | + $value->$foreignKeyName = $model->id; |
|
439 | + $value->save(); |
|
440 | + break; |
|
441 | + case 'BelongsTo': |
|
442 | + /** |
|
443 | + * Save the model. |
|
444 | + */ |
|
445 | + $value->save(); |
|
446 | + $model->$key()->associate($value); |
|
447 | + break; |
|
448 | + } |
|
449 | + } |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Save the model. |
|
454 | + */ |
|
455 | + $model->save(); |
|
456 | + |
|
457 | + return $model; |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * Build the conditions recursively for the retrieving methods. |
|
462 | + * @param array $conditions |
|
463 | + * @return array |
|
464 | + */ |
|
465 | + protected function constructConditions($conditions, $model) |
|
466 | + { |
|
467 | + $conditionString = ''; |
|
468 | + $conditionValues = []; |
|
469 | + foreach ($conditions as $key => $value) { |
|
470 | + if (Str::contains($key, '->')) { |
|
471 | + $key = $this->wrapJsonSelector($key); |
|
472 | + } |
|
473 | + |
|
474 | + if ($key == 'and') { |
|
475 | + $conditions = $this->constructConditions($value, $model); |
|
476 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
477 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
478 | + } elseif ($key == 'or') { |
|
479 | + $conditions = $this->constructConditions($value, $model); |
|
480 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
481 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
482 | + } else { |
|
483 | + if (is_array($value)) { |
|
484 | + $operator = $value['op']; |
|
485 | + if (strtolower($operator) == 'between') { |
|
486 | + $value1 = $value['val1']; |
|
487 | + $value2 = $value['val2']; |
|
488 | + } else { |
|
489 | + $value = Arr::get($value, 'val', ''); |
|
490 | + } |
|
491 | + } else { |
|
492 | + $operator = '='; |
|
493 | + } |
|
494 | 494 | |
495 | - if (strtolower($operator) == 'between') { |
|
496 | - $conditionString .= $key.' >= ? and '; |
|
497 | - $conditionValues[] = $value1; |
|
498 | - |
|
499 | - $conditionString .= $key.' <= ? {op} '; |
|
500 | - $conditionValues[] = $value2; |
|
501 | - } elseif (strtolower($operator) == 'in') { |
|
502 | - $conditionValues = array_merge($conditionValues, $value); |
|
503 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
504 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
505 | - } elseif (strtolower($operator) == 'null') { |
|
506 | - $conditionString .= $key.' is null {op} '; |
|
507 | - } elseif (strtolower($operator) == 'not null') { |
|
508 | - $conditionString .= $key.' is not null {op} '; |
|
509 | - } elseif (strtolower($operator) == 'has') { |
|
510 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
511 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
512 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
513 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
514 | - } else { |
|
515 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
516 | - $conditionValues[] = $value; |
|
517 | - } |
|
518 | - } |
|
519 | - } |
|
520 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
521 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
522 | - } |
|
523 | - |
|
524 | - /** |
|
525 | - * Wrap the given JSON selector. |
|
526 | - * |
|
527 | - * @param string $value |
|
528 | - * @return string |
|
529 | - */ |
|
530 | - protected function wrapJsonSelector($value) |
|
531 | - { |
|
532 | - $removeLast = strpos($value, ')'); |
|
533 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
534 | - $path = explode('->', $value); |
|
535 | - $field = array_shift($path); |
|
536 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
537 | - return '"'.$part.'"'; |
|
538 | - })->implode('.')); |
|
495 | + if (strtolower($operator) == 'between') { |
|
496 | + $conditionString .= $key.' >= ? and '; |
|
497 | + $conditionValues[] = $value1; |
|
498 | + |
|
499 | + $conditionString .= $key.' <= ? {op} '; |
|
500 | + $conditionValues[] = $value2; |
|
501 | + } elseif (strtolower($operator) == 'in') { |
|
502 | + $conditionValues = array_merge($conditionValues, $value); |
|
503 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
504 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
505 | + } elseif (strtolower($operator) == 'null') { |
|
506 | + $conditionString .= $key.' is null {op} '; |
|
507 | + } elseif (strtolower($operator) == 'not null') { |
|
508 | + $conditionString .= $key.' is not null {op} '; |
|
509 | + } elseif (strtolower($operator) == 'has') { |
|
510 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
511 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
512 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
513 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
514 | + } else { |
|
515 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
516 | + $conditionValues[] = $value; |
|
517 | + } |
|
518 | + } |
|
519 | + } |
|
520 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
521 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
522 | + } |
|
523 | + |
|
524 | + /** |
|
525 | + * Wrap the given JSON selector. |
|
526 | + * |
|
527 | + * @param string $value |
|
528 | + * @return string |
|
529 | + */ |
|
530 | + protected function wrapJsonSelector($value) |
|
531 | + { |
|
532 | + $removeLast = strpos($value, ')'); |
|
533 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
534 | + $path = explode('->', $value); |
|
535 | + $field = array_shift($path); |
|
536 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
537 | + return '"'.$part.'"'; |
|
538 | + })->implode('.')); |
|
539 | 539 | |
540 | - return $removeLast === false ? $result : $result.')'; |
|
541 | - } |
|
540 | + return $removeLast === false ? $result : $result.')'; |
|
541 | + } |
|
542 | 542 | } |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | */ |
20 | 20 | public function __construct($model) |
21 | 21 | { |
22 | - $this->model = $model; |
|
22 | + $this->model = $model; |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | /** |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | $model = false; |
85 | 85 | $relations = []; |
86 | 86 | |
87 | - \DB::transaction(function () use (&$model, $relations, $data) { |
|
87 | + \DB::transaction(function() use (&$model, $relations, $data) { |
|
88 | 88 | |
89 | 89 | $model = $this->prepareModel($data); |
90 | 90 | $relations = $this->prepareRelations($data, $model); |
@@ -104,8 +104,8 @@ discard block |
||
104 | 104 | */ |
105 | 105 | public function delete($value, $attribute = 'id') |
106 | 106 | { |
107 | - \DB::transaction(function () use ($value, $attribute) { |
|
108 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
107 | + \DB::transaction(function() use ($value, $attribute) { |
|
108 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function($model) { |
|
109 | 109 | $model->delete(); |
110 | 110 | }); |
111 | 111 | }); |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | { |
196 | 196 | $model = $this->model->onlyTrashed()->find($id); |
197 | 197 | |
198 | - if (! $model) { |
|
198 | + if ( ! $model) { |
|
199 | 199 | \Errors::notFound(class_basename($this->model).' with id : '.$id); |
200 | 200 | } |
201 | 201 | |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | * @var array |
220 | 220 | */ |
221 | 221 | $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
222 | - if (! $model) { |
|
222 | + if ( ! $model) { |
|
223 | 223 | \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
224 | 224 | } |
225 | 225 | |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | * If the relation has no value then marke the relation data |
270 | 270 | * related to the model to be deleted. |
271 | 271 | */ |
272 | - if (! $value || ! count($value)) { |
|
272 | + if ( ! $value || ! count($value)) { |
|
273 | 273 | $relations[$relation] = 'delete'; |
274 | 274 | } |
275 | 275 | } |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | /** |
297 | 297 | * If model doesn't exists. |
298 | 298 | */ |
299 | - if (! $relationModel) { |
|
299 | + if ( ! $relationModel) { |
|
300 | 300 | \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
301 | 301 | } |
302 | 302 | |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | /** |
328 | 328 | * If model doesn't exists. |
329 | 329 | */ |
330 | - if (! $relationModel) { |
|
330 | + if ( ! $relationModel) { |
|
331 | 331 | \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
332 | 332 | } |
333 | 333 | |
@@ -533,7 +533,7 @@ discard block |
||
533 | 533 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
534 | 534 | $path = explode('->', $value); |
535 | 535 | $field = array_shift($path); |
536 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
536 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
537 | 537 | return '"'.$part.'"'; |
538 | 538 | })->implode('.')); |
539 | 539 |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @param array $relations |
54 | 54 | * @param string $sortBy |
55 | - * @param boolean $desc |
|
55 | + * @param integer $desc |
|
56 | 56 | * @param array $columns |
57 | 57 | * @return collection |
58 | 58 | */ |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | * @param integer $perPage |
68 | 68 | * @param array $relations |
69 | 69 | * @param string $sortBy |
70 | - * @param boolean $desc |
|
70 | + * @param integer $desc |
|
71 | 71 | * @param array $columns |
72 | 72 | * @return collection |
73 | 73 | */ |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | * @param integer $perPage |
85 | 85 | * @param array $relations |
86 | 86 | * @param string $sortBy |
87 | - * @param boolean $desc |
|
87 | + * @param integer $desc |
|
88 | 88 | * @param array $columns |
89 | 89 | * @return collection |
90 | 90 | */ |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | * @param array $conditions array of conditions |
139 | 139 | * @param array $relations |
140 | 140 | * @param string $sortBy |
141 | - * @param boolean $desc |
|
141 | + * @param integer $desc |
|
142 | 142 | * @param array $columns |
143 | 143 | * @return collection |
144 | 144 | */ |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | * @param array $conditions array of conditions |
168 | 168 | * @param integer $perPage |
169 | 169 | * @param string $sortBy |
170 | - * @param boolean $desc |
|
170 | + * @param integer $desc |
|
171 | 171 | * @param array $columns |
172 | 172 | * @return collection |
173 | 173 | */ |
@@ -7,183 +7,183 @@ |
||
7 | 7 | |
8 | 8 | abstract class BaseService implements BaseServiceInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * @var object |
|
12 | - */ |
|
13 | - protected $repo; |
|
10 | + /** |
|
11 | + * @var object |
|
12 | + */ |
|
13 | + protected $repo; |
|
14 | 14 | |
15 | - /** |
|
16 | - * Init new object. |
|
17 | - * |
|
18 | - * @param mixed $repo |
|
19 | - * @return void |
|
20 | - */ |
|
21 | - public function __construct($repo) |
|
22 | - { |
|
23 | - $this->repo = new CachingDecorator($repo, \App::make('Illuminate\Contracts\Cache\Repository')); |
|
24 | - } |
|
15 | + /** |
|
16 | + * Init new object. |
|
17 | + * |
|
18 | + * @param mixed $repo |
|
19 | + * @return void |
|
20 | + */ |
|
21 | + public function __construct($repo) |
|
22 | + { |
|
23 | + $this->repo = new CachingDecorator($repo, \App::make('Illuminate\Contracts\Cache\Repository')); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Fetch records with relations based on the given params. |
|
28 | - * |
|
29 | - * @param string $relations |
|
30 | - * @param array $conditions |
|
31 | - * @param integer $perPage |
|
32 | - * @param string $sortBy |
|
33 | - * @param boolean $desc |
|
34 | - * @return collection |
|
35 | - */ |
|
36 | - public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true) |
|
37 | - { |
|
38 | - unset($conditions['perPage']); |
|
39 | - unset($conditions['sortBy']); |
|
40 | - unset($conditions['sort']); |
|
41 | - unset($conditions['page']); |
|
26 | + /** |
|
27 | + * Fetch records with relations based on the given params. |
|
28 | + * |
|
29 | + * @param string $relations |
|
30 | + * @param array $conditions |
|
31 | + * @param integer $perPage |
|
32 | + * @param string $sortBy |
|
33 | + * @param boolean $desc |
|
34 | + * @return collection |
|
35 | + */ |
|
36 | + public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true) |
|
37 | + { |
|
38 | + unset($conditions['perPage']); |
|
39 | + unset($conditions['sortBy']); |
|
40 | + unset($conditions['sort']); |
|
41 | + unset($conditions['page']); |
|
42 | 42 | |
43 | - if (count($conditions)) { |
|
44 | - return $this->repo->paginateBy(['and' => $conditions], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
45 | - } |
|
43 | + if (count($conditions)) { |
|
44 | + return $this->repo->paginateBy(['and' => $conditions], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
45 | + } |
|
46 | 46 | |
47 | - return $this->repo->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
48 | - } |
|
47 | + return $this->repo->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Fetch all records with relations from the storage. |
|
52 | - * |
|
53 | - * @param array $relations |
|
54 | - * @param string $sortBy |
|
55 | - * @param boolean $desc |
|
56 | - * @param array $columns |
|
57 | - * @return collection |
|
58 | - */ |
|
59 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
60 | - { |
|
61 | - return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
62 | - } |
|
50 | + /** |
|
51 | + * Fetch all records with relations from the storage. |
|
52 | + * |
|
53 | + * @param array $relations |
|
54 | + * @param string $sortBy |
|
55 | + * @param boolean $desc |
|
56 | + * @param array $columns |
|
57 | + * @return collection |
|
58 | + */ |
|
59 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
60 | + { |
|
61 | + return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Fetch all records with relations from storage in pages. |
|
66 | - * |
|
67 | - * @param integer $perPage |
|
68 | - * @param array $relations |
|
69 | - * @param string $sortBy |
|
70 | - * @param boolean $desc |
|
71 | - * @param array $columns |
|
72 | - * @return collection |
|
73 | - */ |
|
74 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
75 | - { |
|
76 | - return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
77 | - } |
|
64 | + /** |
|
65 | + * Fetch all records with relations from storage in pages. |
|
66 | + * |
|
67 | + * @param integer $perPage |
|
68 | + * @param array $relations |
|
69 | + * @param string $sortBy |
|
70 | + * @param boolean $desc |
|
71 | + * @param array $columns |
|
72 | + * @return collection |
|
73 | + */ |
|
74 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
75 | + { |
|
76 | + return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Fetch all records with relations based on |
|
81 | - * the given condition from storage in pages. |
|
82 | - * |
|
83 | - * @param array $conditions array of conditions |
|
84 | - * @param integer $perPage |
|
85 | - * @param array $relations |
|
86 | - * @param string $sortBy |
|
87 | - * @param boolean $desc |
|
88 | - * @param array $columns |
|
89 | - * @return collection |
|
90 | - */ |
|
91 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
92 | - { |
|
93 | - return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
94 | - } |
|
79 | + /** |
|
80 | + * Fetch all records with relations based on |
|
81 | + * the given condition from storage in pages. |
|
82 | + * |
|
83 | + * @param array $conditions array of conditions |
|
84 | + * @param integer $perPage |
|
85 | + * @param array $relations |
|
86 | + * @param string $sortBy |
|
87 | + * @param boolean $desc |
|
88 | + * @param array $columns |
|
89 | + * @return collection |
|
90 | + */ |
|
91 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
92 | + { |
|
93 | + return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Save the given model to the storage. |
|
98 | - * |
|
99 | - * @param array $data |
|
100 | - * @return mixed |
|
101 | - */ |
|
102 | - public function save(array $data) |
|
103 | - { |
|
104 | - return $this->repo->save($data); |
|
105 | - } |
|
96 | + /** |
|
97 | + * Save the given model to the storage. |
|
98 | + * |
|
99 | + * @param array $data |
|
100 | + * @return mixed |
|
101 | + */ |
|
102 | + public function save(array $data) |
|
103 | + { |
|
104 | + return $this->repo->save($data); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Delete record from the storage based on the given |
|
109 | - * condition. |
|
110 | - * |
|
111 | - * @param var $value condition value |
|
112 | - * @param string $attribute condition column name |
|
113 | - * @return void |
|
114 | - */ |
|
115 | - public function delete($value, $attribute = 'id') |
|
116 | - { |
|
117 | - return $this->repo->save($value, $attribute); |
|
118 | - } |
|
107 | + /** |
|
108 | + * Delete record from the storage based on the given |
|
109 | + * condition. |
|
110 | + * |
|
111 | + * @param var $value condition value |
|
112 | + * @param string $attribute condition column name |
|
113 | + * @return void |
|
114 | + */ |
|
115 | + public function delete($value, $attribute = 'id') |
|
116 | + { |
|
117 | + return $this->repo->save($value, $attribute); |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * Fetch records from the storage based on the given |
|
122 | - * id. |
|
123 | - * |
|
124 | - * @param integer $id |
|
125 | - * @param string[] $relations |
|
126 | - * @param array $columns |
|
127 | - * @return object |
|
128 | - */ |
|
129 | - public function find($id, $relations = [], $columns = ['*']) |
|
130 | - { |
|
131 | - return $this->repo->find($id, $relations, $columns); |
|
132 | - } |
|
120 | + /** |
|
121 | + * Fetch records from the storage based on the given |
|
122 | + * id. |
|
123 | + * |
|
124 | + * @param integer $id |
|
125 | + * @param string[] $relations |
|
126 | + * @param array $columns |
|
127 | + * @return object |
|
128 | + */ |
|
129 | + public function find($id, $relations = [], $columns = ['*']) |
|
130 | + { |
|
131 | + return $this->repo->find($id, $relations, $columns); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * Fetch records from the storage based on the given |
|
136 | - * condition. |
|
137 | - * |
|
138 | - * @param array $conditions array of conditions |
|
139 | - * @param array $relations |
|
140 | - * @param string $sortBy |
|
141 | - * @param boolean $desc |
|
142 | - * @param array $columns |
|
143 | - * @return collection |
|
144 | - */ |
|
145 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
146 | - { |
|
147 | - return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
148 | - } |
|
134 | + /** |
|
135 | + * Fetch records from the storage based on the given |
|
136 | + * condition. |
|
137 | + * |
|
138 | + * @param array $conditions array of conditions |
|
139 | + * @param array $relations |
|
140 | + * @param string $sortBy |
|
141 | + * @param boolean $desc |
|
142 | + * @param array $columns |
|
143 | + * @return collection |
|
144 | + */ |
|
145 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
146 | + { |
|
147 | + return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Fetch the first record from the storage based on the given |
|
152 | - * condition. |
|
153 | - * |
|
154 | - * @param array $conditions array of conditions |
|
155 | - * @param array $relations |
|
156 | - * @param array $columns |
|
157 | - * @return object |
|
158 | - */ |
|
159 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
160 | - { |
|
161 | - return $this->repo->first($conditions, $relations, $columns); |
|
162 | - } |
|
150 | + /** |
|
151 | + * Fetch the first record from the storage based on the given |
|
152 | + * condition. |
|
153 | + * |
|
154 | + * @param array $conditions array of conditions |
|
155 | + * @param array $relations |
|
156 | + * @param array $columns |
|
157 | + * @return object |
|
158 | + */ |
|
159 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
160 | + { |
|
161 | + return $this->repo->first($conditions, $relations, $columns); |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
165 | - * Return the deleted models in pages based on the given conditions. |
|
166 | - * |
|
167 | - * @param array $conditions array of conditions |
|
168 | - * @param integer $perPage |
|
169 | - * @param string $sortBy |
|
170 | - * @param boolean $desc |
|
171 | - * @param array $columns |
|
172 | - * @return collection |
|
173 | - */ |
|
174 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
175 | - { |
|
176 | - return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
177 | - } |
|
164 | + /** |
|
165 | + * Return the deleted models in pages based on the given conditions. |
|
166 | + * |
|
167 | + * @param array $conditions array of conditions |
|
168 | + * @param integer $perPage |
|
169 | + * @param string $sortBy |
|
170 | + * @param boolean $desc |
|
171 | + * @param array $columns |
|
172 | + * @return collection |
|
173 | + */ |
|
174 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
175 | + { |
|
176 | + return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * Restore the deleted model. |
|
181 | - * |
|
182 | - * @param integer $id |
|
183 | - * @return void |
|
184 | - */ |
|
185 | - public function restore($id) |
|
186 | - { |
|
187 | - return $this->repo->restore($id); |
|
188 | - } |
|
179 | + /** |
|
180 | + * Restore the deleted model. |
|
181 | + * |
|
182 | + * @param integer $id |
|
183 | + * @return void |
|
184 | + */ |
|
185 | + public function restore($id) |
|
186 | + { |
|
187 | + return $this->repo->restore($id); |
|
188 | + } |
|
189 | 189 | } |
@@ -8,36 +8,36 @@ |
||
8 | 8 | class Permission extends Model |
9 | 9 | { |
10 | 10 | |
11 | - use SoftDeletes; |
|
12 | - protected $table = 'permissions'; |
|
13 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
14 | - protected $hidden = ['deleted_at']; |
|
15 | - protected $guarded = ['id']; |
|
16 | - protected $fillable = ['name', 'model']; |
|
11 | + use SoftDeletes; |
|
12 | + protected $table = 'permissions'; |
|
13 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
14 | + protected $hidden = ['deleted_at']; |
|
15 | + protected $guarded = ['id']; |
|
16 | + protected $fillable = ['name', 'model']; |
|
17 | 17 | |
18 | - public function getCreatedAtAttribute($value) |
|
19 | - { |
|
20 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | - } |
|
18 | + public function getCreatedAtAttribute($value) |
|
19 | + { |
|
20 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | + } |
|
22 | 22 | |
23 | - public function getUpdatedAtAttribute($value) |
|
24 | - { |
|
25 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
26 | - } |
|
23 | + public function getUpdatedAtAttribute($value) |
|
24 | + { |
|
25 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
26 | + } |
|
27 | 27 | |
28 | - public function getDeletedAtAttribute($value) |
|
29 | - { |
|
30 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
31 | - } |
|
28 | + public function getDeletedAtAttribute($value) |
|
29 | + { |
|
30 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
31 | + } |
|
32 | 32 | |
33 | - public function roles() |
|
34 | - { |
|
35 | - return $this->belongsToMany(Role::class, 'permission_role', 'permission_id', 'role_id')->whereNull('permission_role.deleted_at')->withTimestamps(); |
|
36 | - } |
|
33 | + public function roles() |
|
34 | + { |
|
35 | + return $this->belongsToMany(Role::class, 'permission_role', 'permission_id', 'role_id')->whereNull('permission_role.deleted_at')->withTimestamps(); |
|
36 | + } |
|
37 | 37 | |
38 | - public static function boot() |
|
39 | - { |
|
40 | - parent::boot(); |
|
41 | - Permission::observe(PermissionObserver::class); |
|
42 | - } |
|
38 | + public static function boot() |
|
39 | + { |
|
40 | + parent::boot(); |
|
41 | + Permission::observe(PermissionObserver::class); |
|
42 | + } |
|
43 | 43 | } |
@@ -8,47 +8,47 @@ |
||
8 | 8 | class PushNotificationDevice extends Model |
9 | 9 | { |
10 | 10 | |
11 | - use SoftDeletes; |
|
12 | - protected $table = 'push_notification_devices'; |
|
13 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
14 | - protected $hidden = ['deleted_at', 'access_token']; |
|
15 | - protected $guarded = ['id']; |
|
16 | - protected $fillable = ['device_token', 'user_id', 'access_token']; |
|
17 | - |
|
18 | - public function getCreatedAtAttribute($value) |
|
19 | - { |
|
20 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | - } |
|
22 | - |
|
23 | - public function getUpdatedAtAttribute($value) |
|
24 | - { |
|
25 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
26 | - } |
|
27 | - |
|
28 | - public function getDeletedAtAttribute($value) |
|
29 | - { |
|
30 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
31 | - } |
|
11 | + use SoftDeletes; |
|
12 | + protected $table = 'push_notification_devices'; |
|
13 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
14 | + protected $hidden = ['deleted_at', 'access_token']; |
|
15 | + protected $guarded = ['id']; |
|
16 | + protected $fillable = ['device_token', 'user_id', 'access_token']; |
|
17 | + |
|
18 | + public function getCreatedAtAttribute($value) |
|
19 | + { |
|
20 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | + } |
|
22 | + |
|
23 | + public function getUpdatedAtAttribute($value) |
|
24 | + { |
|
25 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
26 | + } |
|
27 | + |
|
28 | + public function getDeletedAtAttribute($value) |
|
29 | + { |
|
30 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
31 | + } |
|
32 | 32 | |
33 | - public function user() |
|
34 | - { |
|
35 | - return $this->belongsTo(AclUser::class); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Encrypt the access_token attribute before |
|
40 | - * saving it in the storage. |
|
41 | - * |
|
42 | - * @param string $value |
|
43 | - */ |
|
44 | - public function setLoginTokenAttribute($value) |
|
45 | - { |
|
46 | - $this->attributes['access_token'] = encrypt($value); |
|
47 | - } |
|
48 | - |
|
49 | - public static function boot() |
|
50 | - { |
|
51 | - parent::boot(); |
|
52 | - PushNotificationDevice::observe(PushNotificationDeviceObserver::class); |
|
53 | - } |
|
33 | + public function user() |
|
34 | + { |
|
35 | + return $this->belongsTo(AclUser::class); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Encrypt the access_token attribute before |
|
40 | + * saving it in the storage. |
|
41 | + * |
|
42 | + * @param string $value |
|
43 | + */ |
|
44 | + public function setLoginTokenAttribute($value) |
|
45 | + { |
|
46 | + $this->attributes['access_token'] = encrypt($value); |
|
47 | + } |
|
48 | + |
|
49 | + public static function boot() |
|
50 | + { |
|
51 | + parent::boot(); |
|
52 | + PushNotificationDevice::observe(PushNotificationDeviceObserver::class); |
|
53 | + } |
|
54 | 54 | } |
@@ -8,51 +8,51 @@ |
||
8 | 8 | |
9 | 9 | class SetSessions |
10 | 10 | { |
11 | - protected $app; |
|
12 | - protected $session; |
|
11 | + protected $app; |
|
12 | + protected $session; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Init new object. |
|
16 | - * |
|
17 | - * @param App $app |
|
18 | - * @param Session $session |
|
19 | - * |
|
20 | - * @return void |
|
21 | - */ |
|
22 | - public function __construct(App $app, Session $session) |
|
23 | - { |
|
24 | - $this->app = $app; |
|
25 | - $this->session = $session; |
|
26 | - } |
|
14 | + /** |
|
15 | + * Init new object. |
|
16 | + * |
|
17 | + * @param App $app |
|
18 | + * @param Session $session |
|
19 | + * |
|
20 | + * @return void |
|
21 | + */ |
|
22 | + public function __construct(App $app, Session $session) |
|
23 | + { |
|
24 | + $this->app = $app; |
|
25 | + $this->session = $session; |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Handle an incoming request. |
|
30 | - * |
|
31 | - * @param \Illuminate\Http\Request $request |
|
32 | - * @param \Closure $next |
|
33 | - * @return mixed |
|
34 | - */ |
|
35 | - public function handle($request, Closure $next) |
|
36 | - { |
|
37 | - $this->session->put('time-zone', $request->header('time-zone') ?: 0); |
|
28 | + /** |
|
29 | + * Handle an incoming request. |
|
30 | + * |
|
31 | + * @param \Illuminate\Http\Request $request |
|
32 | + * @param \Closure $next |
|
33 | + * @return mixed |
|
34 | + */ |
|
35 | + public function handle($request, Closure $next) |
|
36 | + { |
|
37 | + $this->session->put('time-zone', $request->header('time-zone') ?: 0); |
|
38 | 38 | |
39 | - $locale = $request->header('locale'); |
|
40 | - switch ($locale) { |
|
41 | - case 'en': |
|
42 | - $this->app->setLocale('en'); |
|
43 | - $this->session->put('locale', 'en'); |
|
44 | - break; |
|
39 | + $locale = $request->header('locale'); |
|
40 | + switch ($locale) { |
|
41 | + case 'en': |
|
42 | + $this->app->setLocale('en'); |
|
43 | + $this->session->put('locale', 'en'); |
|
44 | + break; |
|
45 | 45 | |
46 | - case 'ar': |
|
47 | - $this->app->setLocale('ar'); |
|
48 | - $this->session->put('locale', 'ar'); |
|
49 | - break; |
|
46 | + case 'ar': |
|
47 | + $this->app->setLocale('ar'); |
|
48 | + $this->session->put('locale', 'ar'); |
|
49 | + break; |
|
50 | 50 | |
51 | - default: |
|
52 | - $this->app->setLocale('en'); |
|
53 | - $this->session->put('locale', 'en'); |
|
54 | - break; |
|
55 | - } |
|
56 | - return $next($request); |
|
57 | - } |
|
51 | + default: |
|
52 | + $this->app->setLocale('en'); |
|
53 | + $this->session->put('locale', 'en'); |
|
54 | + break; |
|
55 | + } |
|
56 | + return $next($request); |
|
57 | + } |
|
58 | 58 | } |
@@ -6,31 +6,31 @@ |
||
6 | 6 | |
7 | 7 | class DummyModel extends Model |
8 | 8 | { |
9 | - use SoftDeletes; |
|
10 | - protected $table = 'DummyTableName'; |
|
11 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
12 | - protected $hidden = ['deleted_at']; |
|
13 | - protected $guarded = ['id']; |
|
14 | - protected $fillable = []; // Add attributes here |
|
9 | + use SoftDeletes; |
|
10 | + protected $table = 'DummyTableName'; |
|
11 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
12 | + protected $hidden = ['deleted_at']; |
|
13 | + protected $guarded = ['id']; |
|
14 | + protected $fillable = []; // Add attributes here |
|
15 | 15 | |
16 | - public function getCreatedAtAttribute($value) |
|
17 | - { |
|
18 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
19 | - } |
|
16 | + public function getCreatedAtAttribute($value) |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
24 | - } |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
24 | + } |
|
25 | 25 | |
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
29 | - } |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
29 | + } |
|
30 | 30 | |
31 | - public static function boot() |
|
32 | - { |
|
33 | - parent::boot(); |
|
34 | - DummyModel::observe(DummyObserver::class); |
|
35 | - } |
|
31 | + public static function boot() |
|
32 | + { |
|
33 | + parent::boot(); |
|
34 | + DummyModel::observe(DummyObserver::class); |
|
35 | + } |
|
36 | 36 | } |
@@ -7,36 +7,36 @@ |
||
7 | 7 | class Setting extends Model |
8 | 8 | { |
9 | 9 | |
10 | - use SoftDeletes; |
|
11 | - protected $table = 'settings'; |
|
12 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
13 | - protected $hidden = ['deleted_at']; |
|
14 | - protected $guarded = ['id', 'key']; |
|
15 | - protected $fillable = ['name', 'value']; |
|
10 | + use SoftDeletes; |
|
11 | + protected $table = 'settings'; |
|
12 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
13 | + protected $hidden = ['deleted_at']; |
|
14 | + protected $guarded = ['id', 'key']; |
|
15 | + protected $fillable = ['name', 'value']; |
|
16 | 16 | |
17 | - public function getCreatedAtAttribute($value) |
|
18 | - { |
|
19 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
20 | - } |
|
17 | + public function getCreatedAtAttribute($value) |
|
18 | + { |
|
19 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
20 | + } |
|
21 | 21 | |
22 | - public function getUpdatedAtAttribute($value) |
|
23 | - { |
|
24 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
25 | - } |
|
22 | + public function getUpdatedAtAttribute($value) |
|
23 | + { |
|
24 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
25 | + } |
|
26 | 26 | |
27 | - public function getDeletedAtAttribute($value) |
|
28 | - { |
|
29 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
30 | - } |
|
27 | + public function getDeletedAtAttribute($value) |
|
28 | + { |
|
29 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
30 | + } |
|
31 | 31 | |
32 | - public function newCollection(array $models = []) |
|
33 | - { |
|
34 | - return parent::newCollection($models)->keyBy('key'); |
|
35 | - } |
|
32 | + public function newCollection(array $models = []) |
|
33 | + { |
|
34 | + return parent::newCollection($models)->keyBy('key'); |
|
35 | + } |
|
36 | 36 | |
37 | - public static function boot() |
|
38 | - { |
|
39 | - parent::boot(); |
|
40 | - Setting::observe(SettingsObserver::class); |
|
41 | - } |
|
37 | + public static function boot() |
|
38 | + { |
|
39 | + parent::boot(); |
|
40 | + Setting::observe(SettingsObserver::class); |
|
41 | + } |
|
42 | 42 | } |
@@ -8,27 +8,27 @@ |
||
8 | 8 | |
9 | 9 | class OauthClient extends Client |
10 | 10 | { |
11 | - protected $dates = ['created_at', 'updated_at']; |
|
12 | - protected $fillable = ['name', 'redirect', 'user_id', 'personal_access_client', 'password_client', 'revoked']; |
|
11 | + protected $dates = ['created_at', 'updated_at']; |
|
12 | + protected $fillable = ['name', 'redirect', 'user_id', 'personal_access_client', 'password_client', 'revoked']; |
|
13 | 13 | |
14 | - public function getCreatedAtAttribute($value) |
|
15 | - { |
|
16 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
17 | - } |
|
14 | + public function getCreatedAtAttribute($value) |
|
15 | + { |
|
16 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
17 | + } |
|
18 | 18 | |
19 | - public function getUpdatedAtAttribute($value) |
|
20 | - { |
|
21 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
22 | - } |
|
19 | + public function getUpdatedAtAttribute($value) |
|
20 | + { |
|
21 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
22 | + } |
|
23 | 23 | |
24 | - public function user() |
|
25 | - { |
|
26 | - return $this->belongsTo(AclUser::class); |
|
27 | - } |
|
24 | + public function user() |
|
25 | + { |
|
26 | + return $this->belongsTo(AclUser::class); |
|
27 | + } |
|
28 | 28 | |
29 | - public static function boot() |
|
30 | - { |
|
31 | - parent::boot(); |
|
32 | - OauthClient::observe(OauthClientObserver::class); |
|
33 | - } |
|
29 | + public static function boot() |
|
30 | + { |
|
31 | + parent::boot(); |
|
32 | + OauthClient::observe(OauthClientObserver::class); |
|
33 | + } |
|
34 | 34 | } |
@@ -9,41 +9,41 @@ |
||
9 | 9 | class Role extends Model |
10 | 10 | { |
11 | 11 | |
12 | - use SoftDeletes; |
|
13 | - protected $table = 'roles'; |
|
14 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
15 | - protected $hidden = ['deleted_at']; |
|
16 | - protected $guarded = ['id']; |
|
17 | - protected $fillable = ['name']; |
|
18 | - |
|
19 | - public function getCreatedAtAttribute($value) |
|
20 | - { |
|
21 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
22 | - } |
|
23 | - |
|
24 | - public function getUpdatedAtAttribute($value) |
|
25 | - { |
|
26 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
27 | - } |
|
28 | - |
|
29 | - public function getDeletedAtAttribute($value) |
|
30 | - { |
|
31 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
32 | - } |
|
33 | - |
|
34 | - public function users() |
|
35 | - { |
|
36 | - return $this->belongsToMany(AclUser::class, 'role_user', 'role_id', 'user_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
37 | - } |
|
38 | - |
|
39 | - public function permissions() |
|
40 | - { |
|
41 | - return $this->belongsToMany(Permission::class, 'permission_role', 'role_id', 'permission_id')->whereNull('permission_role.deleted_at')->withTimestamps(); |
|
42 | - } |
|
43 | - |
|
44 | - public static function boot() |
|
45 | - { |
|
46 | - parent::boot(); |
|
47 | - Role::observe(RoleObserver::class); |
|
48 | - } |
|
12 | + use SoftDeletes; |
|
13 | + protected $table = 'roles'; |
|
14 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
15 | + protected $hidden = ['deleted_at']; |
|
16 | + protected $guarded = ['id']; |
|
17 | + protected $fillable = ['name']; |
|
18 | + |
|
19 | + public function getCreatedAtAttribute($value) |
|
20 | + { |
|
21 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
22 | + } |
|
23 | + |
|
24 | + public function getUpdatedAtAttribute($value) |
|
25 | + { |
|
26 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
27 | + } |
|
28 | + |
|
29 | + public function getDeletedAtAttribute($value) |
|
30 | + { |
|
31 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
32 | + } |
|
33 | + |
|
34 | + public function users() |
|
35 | + { |
|
36 | + return $this->belongsToMany(AclUser::class, 'role_user', 'role_id', 'user_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
37 | + } |
|
38 | + |
|
39 | + public function permissions() |
|
40 | + { |
|
41 | + return $this->belongsToMany(Permission::class, 'permission_role', 'role_id', 'permission_id')->whereNull('permission_role.deleted_at')->withTimestamps(); |
|
42 | + } |
|
43 | + |
|
44 | + public static function boot() |
|
45 | + { |
|
46 | + parent::boot(); |
|
47 | + Role::observe(RoleObserver::class); |
|
48 | + } |
|
49 | 49 | } |