@@ -43,7 +43,7 @@ public function __construct(Request $request)
4343 /**
4444 * Return the model to use for this controller.
4545 *
46- * @return string
46+ * @return \Illuminate\Database\Eloquent\Model
4747 */
4848 abstract protected function model ();
4949
@@ -65,24 +65,6 @@ public function index(Request $request)
6565 return $ this ->response ($ this ->model ()->paginate ());
6666 }
6767
68- /**
69- * GET: Return a model by ID.
70- *
71- * @param int $id
72- * @param Request $request
73- * @return JsonResponse|Response
74- */
75- public function fetch ($ id , Request $ request )
76- {
77- $ model = $ this ->model ()->find ($ id );
78-
79- if (is_null ($ model ) || !$ model ->exists ) {
80- return $ this ->notFoundResponse ();
81- }
82-
83- return $ this ->response ($ model );
84- }
85-
8668 /**
8769 * PATCH: Update a model.
8870 *
@@ -92,7 +74,7 @@ public function fetch($id, Request $request)
9274 */
9375 public function update ($ id , Request $ request )
9476 {
95- return $ this ->updateAttributes ($ this ->model ()->find ($ id ), $ request ->all (), 'edit ' );
77+ return $ this ->updateModel ($ this ->model ()->find ($ id ), $ request ->all (), 'edit ' );
9678 }
9779
9880 /**
@@ -104,27 +86,17 @@ public function update($id, Request $request)
10486 */
10587 public function destroy ($ id , Request $ request )
10688 {
107- $ this ->validate ( $ request , [ ' force ' => [ ' boolean ' ]] );
89+ $ model = $ this ->model ( );
10890
109- $ model = $ this ->model ()->withTrashed ()->find ($ id );
91+ $ force = false ;
92+ if (method_exists ($ model , 'forceDelete ' )) {
93+ $ this ->validate ($ request , ['force ' => ['boolean ' ]]);
11094
111- if ( is_null ( $ model) || ! $ model ->exists ) {
112- return $ this -> notFoundResponse ( );
95+ $ model = $ model ->withTrashed ();
96+ $ force = ( bool ) $ request -> input ( ' force ' );
11397 }
11498
115- $ model ->timestamps = false ;
116-
117- if ($ request ->has ('force ' ) && $ request ->input ('force ' ) == 1 ) {
118- $ model ->forceDelete ();
119- return $ this ->response ($ model , $ this ->trans ('perma_deleted ' ));
120- } elseif (!$ model ->trashed ()) {
121- $ model ->delete ();
122- return $ this ->response ($ model , $ this ->trans ('deleted ' ));
123- }
124-
125- $ model ->timestamps = true ;
126-
127- return $ this ->notFoundResponse ();
99+ return $ this ->deleteModel ($ model ->find ($ id ), 'delete ' , $ force );
128100 }
129101
130102 /**
@@ -195,6 +167,10 @@ protected function bulk(Request $request, $action, $transKey, array $input = [])
195167 foreach ($ items as $ id ) {
196168 $ response = $ this ->{$ action }($ id , $ request );
197169
170+ if ($ response ->isClientError ()) {
171+ return $ response ;
172+ }
173+
198174 if (!$ response ->isNotFound ()) {
199175 $ models ->push ($ response ->getOriginalContent ());
200176 }
@@ -211,12 +187,57 @@ protected function bulk(Request $request, $action, $transKey, array $input = [])
211187 * @param array|string $authorize
212188 * @return JsonResponse|Response
213189 */
214- protected function updateAttributes ($ model , array $ attributes , $ authorize = [])
190+ protected function updateModel ($ model , array $ attributes , $ authorize = [])
215191 {
216192 if (is_null ($ model ) || !$ model ->exists ) {
217193 return $ this ->notFoundResponse ();
218194 }
219195
196+ $ this ->parseAuthorization ($ model , $ authorize );
197+
198+ $ model ->update ($ attributes );
199+
200+ return $ this ->response ($ model , $ this ->trans ('updated ' ));
201+ }
202+
203+ /**
204+ * Delete a model.
205+ *
206+ * @param Model $model
207+ * @param array|string $authorize
208+ * @param bool $force
209+ * @return JsonResponse|Response
210+ */
211+ protected function deleteModel ($ model , $ authorize = [], $ force = false )
212+ {
213+ if (is_null ($ model ) || !$ model ->exists ) {
214+ return $ this ->notFoundResponse ();
215+ }
216+
217+ $ this ->parseAuthorization ($ model , $ authorize );
218+
219+ if ($ force ) {
220+ $ model ->forceDelete ();
221+
222+ return $ this ->response ($ model , $ this ->trans ('perma_deleted ' ));
223+ } else {
224+ $ model ->timestamps = false ;
225+ $ model ->delete ();
226+ $ model ->timestamps = true ;
227+
228+ return $ this ->response ($ model , $ this ->trans ('deleted ' ));
229+ }
230+ }
231+
232+ /**
233+ * Parse an authorization parameter and authorize if applicable.
234+ *
235+ * @param Model $model
236+ * @param array|string $authorize
237+ * @return JsonResponse|Response
238+ */
239+ protected function parseAuthorization ($ model , $ authorize = [])
240+ {
220241 if (!empty ($ authorize )) {
221242 // We need to authorize this change
222243
@@ -229,10 +250,6 @@ protected function updateAttributes($model, array $attributes, $authorize = [])
229250
230251 $ this ->authorize ($ ability , $ authorizeModel );
231252 }
232-
233- $ model ->update ($ attributes );
234-
235- return $ this ->response ($ model , $ this ->trans ('updated ' ));
236253 }
237254
238255 /**
@@ -292,14 +309,14 @@ protected function notFoundResponse()
292309 * Create the response for when a request fails validation.
293310 *
294311 * @param Request $request
295- * @param array $errors
312+ * @param array|string $errors
296313 * @return JsonResponse|Response
297314 */
298- protected function buildFailedValidationResponse (Request $ request , array $ errors )
315+ protected function buildFailedValidationResponse (Request $ request , $ errors )
299316 {
300317 $ content = [
301318 'error ' => "The submitted data did not pass validation. " ,
302- 'validation_errors ' => $ errors
319+ 'validation_errors ' => ( array ) $ errors
303320 ];
304321
305322 return ($ request ->ajax () || $ request ->wantsJson ())
0 commit comments