-
app/Http/Controllers/MediaController.php
Open in GitHubuse Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\DB; use App\Models\Media; class MediaController extends Controller { // ... public function sort(Request $request) { $request->validate([ 'medias' => ['required', 'array'], 'model' => 'required', 'collection' => 'required', ]); DB::beginTransaction(); try { foreach ($request->medias as $sort => $media) { $media = Media::where('id', $media['id']) ->where('model_type', $request->model) ->where('collection_name', $request->collection) ->firstOrFail(); $media->order_column = $sort + 1; $media->save(); } DB::commit(); return response()->json(); } catch (\Exception $e) { DB::rollBack(); Log::error($e); return response()->json(['status' => 'error', 'message' => 'Could not sort update, please try again.'], 500); } } // ... }