@@ 52-102 (lines=51) @@ | ||
49 | * @param Request $request |
|
50 | * @return JsonResponse |
|
51 | */ |
|
52 | public function create(Request $request) |
|
53 | { |
|
54 | $instance = $this->resolveModel(); |
|
55 | $connection = $instance->getConnection(); |
|
56 | $affected = []; |
|
57 | $errors = []; |
|
58 | ||
59 | $connection->beginTransaction(); |
|
60 | foreach ($request->get('data') as $data) { |
|
61 | $validator = $this->getValidationFactory() |
|
62 | ->make($data, $this->createRules(), $this->createMessages(), $this->attributes()); |
|
63 | if ($validator->fails()) { |
|
64 | foreach ($this->formatErrors($validator) as $error) { |
|
65 | $errors[] = $error; |
|
66 | } |
|
67 | ||
68 | continue; |
|
69 | } |
|
70 | ||
71 | $instance->fill($data); |
|
72 | ||
73 | if (method_exists($this, 'creating')) { |
|
74 | $data = $this->creating($instance, $data); |
|
75 | } |
|
76 | ||
77 | if (method_exists($this, 'saving')) { |
|
78 | $data = $this->saving($instance, $data); |
|
79 | } |
|
80 | ||
81 | $instance->save(); |
|
82 | ||
83 | if (method_exists($this, 'created')) { |
|
84 | $instance = $this->created($instance, $data); |
|
85 | } |
|
86 | ||
87 | if (method_exists($this, 'saved')) { |
|
88 | $instance = $this->saved($instance, $data); |
|
89 | } |
|
90 | ||
91 | $instance->setAttribute('DT_RowId', $instance->getKey()); |
|
92 | $affected[] = $instance; |
|
93 | } |
|
94 | ||
95 | if (! $errors) { |
|
96 | $connection->commit(); |
|
97 | } else { |
|
98 | $connection->rollBack(); |
|
99 | } |
|
100 | ||
101 | return $this->toJson($affected, $errors); |
|
102 | } |
|
103 | ||
104 | /** |
|
105 | * Resolve model to used. |
|
@@ 186-237 (lines=52) @@ | ||
183 | * @param Request $request |
|
184 | * @return JsonResponse |
|
185 | */ |
|
186 | public function edit(Request $request) |
|
187 | { |
|
188 | $connection = $this->getBuilder()->getConnection(); |
|
189 | $affected = []; |
|
190 | $errors = []; |
|
191 | ||
192 | ||
193 | $connection->beginTransaction(); |
|
194 | foreach ($request->get('data') as $key => $data) { |
|
195 | $model = $this->getBuilder()->findOrFail($key); |
|
196 | $validator = $this->getValidationFactory() |
|
197 | ->make($data, $this->editRules($model), $this->editMessages(), $this->attributes()); |
|
198 | if ($validator->fails()) { |
|
199 | foreach ($this->formatErrors($validator) as $error) { |
|
200 | $errors[] = $error; |
|
201 | } |
|
202 | ||
203 | continue; |
|
204 | } |
|
205 | ||
206 | $model->fill($data); |
|
207 | ||
208 | if (method_exists($this, 'updating')) { |
|
209 | $data = $this->updating($model, $data); |
|
210 | } |
|
211 | ||
212 | if (method_exists($this, 'saving')) { |
|
213 | $data = $this->saving($model, $data); |
|
214 | } |
|
215 | ||
216 | $model->save(); |
|
217 | ||
218 | if (method_exists($this, 'updated')) { |
|
219 | $model = $this->updated($model, $data); |
|
220 | } |
|
221 | ||
222 | if (method_exists($this, 'saved')) { |
|
223 | $model = $this->saved($model, $data); |
|
224 | } |
|
225 | ||
226 | $model->setAttribute('DT_RowId', $model->getKey()); |
|
227 | $affected[] = $model; |
|
228 | } |
|
229 | ||
230 | if (! $errors) { |
|
231 | $connection->commit(); |
|
232 | } else { |
|
233 | $connection->rollBack(); |
|
234 | } |
|
235 | ||
236 | return $this->toJson($affected, $errors); |
|
237 | } |
|
238 | ||
239 | /** |
|
240 | * Get elqouent builder of the model. |