From fcb54717ed8ff389b6f7781bbf7194cf58a78834 Mon Sep 17 00:00:00 2001 From: saparatayev Date: Fri, 1 Jul 2022 17:36:27 +0500 Subject: [PATCH] fix: when uploading for a full group --- app/Http/Controllers/Web/TradingController.php | 15 ++++++++++++++- app/Models/Group.php | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Web/TradingController.php b/app/Http/Controllers/Web/TradingController.php index 928ceb9..94719fb 100644 --- a/app/Http/Controllers/Web/TradingController.php +++ b/app/Http/Controllers/Web/TradingController.php @@ -9,6 +9,8 @@ use Maatwebsite\Excel\Facades\Excel; use App\Http\Resources\SubgroupResource; use App\Imports\TradingsImport; use App\Models\Subgroup; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; class TradingController extends Controller { @@ -75,7 +77,18 @@ class TradingController extends Controller ]); } - $group->tradings()->whereLocale(app()->getLocale())->delete(); + DB::beginTransaction(); + + try { + $group->tradings()->whereLocale(app()->getLocale())->delete(); + $group->subgroups()->whereLocale(app()->getLocale())->delete(); + } catch (\Throwable $th) { + Log::info($th); + DB::rollback(); + return redirect()->route('tradings'); + } + + DB::commit(); try { $id = now()->unix(); diff --git a/app/Models/Group.php b/app/Models/Group.php index e9ae5e2..9715bb1 100644 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -39,6 +39,11 @@ class Group extends Model return $this->hasMany(Import::class); } + public function subgroups() + { + return $this->hasMany(Subgroup::class); + } + public function getFilenameAttribute() { $locale = app()->getLocale();