Browse Source

update delete relative company

pull/1/head
wahyuun 10 months ago
parent
commit
1f3eb8576a
  1. 39
      app/Http/Controllers/CompanyController.php

39
app/Http/Controllers/CompanyController.php

@ -2,9 +2,9 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Company; use App\Models\{User, RoleMenu, Role, ProductTransaction, MenuCompany, Company};
use App\Models\MenuCompany;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CompanyController extends Controller class CompanyController extends Controller
{ {
@ -66,8 +66,6 @@ class CompanyController extends Controller
return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400);
die(); die();
} }
if ($result) { if ($result) {
return response()->json(['status' => 'success', 'message' => 'data Company successfully updated!', 'code' => 200], 200); return response()->json(['status' => 'success', 'message' => 'data Company successfully updated!', 'code' => 200], 200);
} else { } else {
@ -77,24 +75,37 @@ class CompanyController extends Controller
public function delete($id) public function delete($id)
{ {
$data = Company::find($id); DB::beginTransaction();
$data = Company::find((int)$id);
if ($data) { if ($data) {
MenuCompany::where('company_id',$id)->delete(); $this->deleteRelative((int)$data['id']);
$delete = $data->delete(); if ($data->delete()) {
DB::commit();
return response()->json(['status' => 'success', 'message' => 'Data Company successfully deleted!', 'code' => 200], 200);
} else {
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Data Company failed deleted!', 'code' => 400], 400);
}
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400); DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Data Company not found!', 'code' => 400], 400);
die(); die();
} }
}
private function deleteRelative($company_id)
if ($delete) { {
return response()->json(['status' => 'success', 'message' => 'data Company successfully deleted!', 'code' => 200], 200); MenuCompany::where('company_id', $company_id)->delete();
} else { User::where('company_id', $company_id)->delete();
return response()->json(['status' => 'failed', 'message' => 'data Company failed deleted!', 'code' => 400], 400); $role = Role::where('company_id', $company_id)->first();
if ($role) {
RoleMenu::where('role_id', $role->id)->delete();
$role->delete();
} }
ProductTransaction::where('company_id', $company_id)->delete();
} }
public function search(Request $request) public function search(Request $request)
{ {
$payload = $request->all(); $payload = $request->all();

Loading…
Cancel
Save