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