davide.cucurnia@vola.it
2024-02-06 91fb11c85ffea90f52af9ad071c997ad2b53a624
commit | author | age
9f6455 1 <?php
DC 2
3 namespace App\Traits;
4
5 use Exception;
6 use Illuminate\Database\Eloquent\Model;
7
8 trait Repository
9 {
10     /**
11      * Eloquent model
12      */
13     protected $modelInstance;
14
15     /**
16      * Get the record with the given id
17      *
18      * @param int $id
19      * @param bool $trashed
20      * @param array $relations
21      * @return Model
22      * @throws Exception
23      */
24     public function get(int $id, bool $trashed = false, array $relations = []) : Model
25     {
26         try {
27             $modelInstance = ($trashed) ?
28                 $this->modelInstance->withTrashed()->findOrFail($id) : $this->modelInstance->findOrFail($id);
29
30             if(!empty($relations)) {
31                 $modelInstance->load($relations);
32             }
33
34             return $modelInstance;
35         } catch (Exception $e) {
36             throw ($e);
37         }
38     }
39
40     /**
41      * Update the record with the given id
42      *
43      * @param array $data
44      * @param Model $instance
45      * @return bool
46      * @throws Exception
47      */
48     public function update(array $data, Model $instance)
49     {
50         try {
51             return $instance->update($data);
52         } catch (Exception $e) {
53             throw ($e);
54         }
55     }
56
57     /**
58      * @param array $params
59      * @param bool $onlyTrashed
60      * @param array $relations
61      * @param bool $paginate
62      * @param bool $withTrashed
63      * @return mixed
64      * @throws Exception
65      */
66     public function search(array $params = [], bool $onlyTrashed = false, array $relations = [], bool $paginate = false, $withTrashed = false, $groupBy = null)
67     {
68         try {
69             $query = $this->modelInstance::searchWithParams($params);
70
71             if($withTrashed) {
72                 $query->withTrashed();
73             } elseif($onlyTrashed) {
74                 $query->onlyTrashed();
75             }
76
77             if(!empty($relations)) {
78                 $query->with($relations);
79             }
80
81             if(!empty($groupBy)) {
82                 $query->groupBy($groupBy);
83             }
84
85             return ($paginate) ? $query->paginate($this->modelInstance->perPage) : $query->get();
86         } catch (Exception $e) {
87             throw ($e);
88         }
89     }
90
91     /**
92      * @param int $id
93      * @return bool|null
94      * @throws Exception
95      */
96     public function destroy(int $id)
97     {
98         try {
99             return $this->get($id)->delete();
100         } catch (Exception $e) {
101             throw ($e);
102         }
103     }
104
105     /**
106      * @param Model $instance
107      * @return bool|null
108      * @throws Exception
109      */
110     public function restore(Model $instance)
111     {
112         try {
113             return $instance->restore();
114         } catch (Exception $e) {
115             throw ($e);
116         }
117     }
118
119     /**
120      * @param array $params
121      * @return mixed
122      * @throws Exception
123      */
124     public function store(array $params)
125     {
126         try {
127             return $this->modelInstance->create($params);
128         } catch (Exception $e) {
129             throw ($e);
130         }
131     }
132
133     /**
134      * @throws Exception
135      */
136     public function beginTransaction()
137     {
138         try {
139             \DB::beginTransaction();
140         } catch (Exception $e) {
141             throw ($e);
142         }
143     }
144
145     /**
146      * @throws Exception
147      */
148     public function commitTransaction()
149     {
150         try {
151             \DB::commit();
152         } catch (Exception $e) {
153             throw ($e);
154         }
155     }
156
157     /**
158      * @throws Exception
159      */
160     public function rollbackTransaction()
161     {
162         try {
163             \DB::rollback();
164         } catch (Exception $e) {
165             throw ($e);
166         }
167     }
168
169     /**
170      * @param Model $model
171      * @param array $relations
172      * @return Model
173      * @throws Exception
174      */
175     public function load(Model $model, array $relations)
176     {
177         try {
178             $model->load($relations);
179
180             return $model;
181         } catch (Exception $e) {
182             throw ($e);
183         }
184     }
185
186     /**
187      * @param array $attributes
188      * @param array $params
189      * @return mixed
190      * @throws Exception
191      */
192     public function firstOrCreate(array $attributes, array $params)
193     {
194         try {
195             return $this->modelInstance->firstOrCreate($attributes, $params);
196         } catch (\Exception $e) {
197             throw ($e);
198         }
199     }
200
201     /**
202      * @param array $params
203      * @return mixed
204      * @throws Exception
205      */
206     public function deleteByCondition(array $params)
207     {
208         try {
209             $queryBuilder = $this->modelInstance;
210             foreach($params as $fields => $value) {
211                 $queryBuilder = $queryBuilder->where($fields, "=", $value);
212             }
213             return $queryBuilder->delete();
214         } catch (\Exception $e) {
215             throw ($e);
216         }
217     }
218
219     /**
220      * @param array $attributes
221      * @param array $params
222      * @param bool $withTrashed
223      * @return mixed
224      * @throws Exception
225      */
226     public function updateOrCreate(array $attributes, array $params, $withTrashed = false)
227     {
228         try {
229             return ($withTrashed) ?  $this->modelInstance->withTrashed()->updateOrCreate($attributes, $params)
230                 : $this->modelInstance->updateOrCreate($attributes, $params);
231         } catch (\Exception $e) {
232             throw ($e);
233         }
234     }
235
236     /**
237      * @param Model $model
238      * @param $relation
239      * @param $values
240      * @return mixed
241      * @throws Exception
242      */
243     public function syncRelations(Model $model, $relation, array $values)
244     {
245         try {
246             return $model->{$relation}()->sync($values);
247         } catch (\Exception $e) {
248             throw ($e);
249         }
250     }
251
252     /**
253      * @throws Exception
254      */
255     public function truncate() {
256         try {
257             $this->modelInstance->whereNotNull('id')->delete();
258         } catch (\Exception $e) {
259             throw($e);
260         }
261     }
262 }