Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function import(Request $request) {
- $inserted_data = 0;
- try {
- for($i = 0; $i < count($request->all()); $i++) {
- // Header ga dimasukin
- if($i > 0) {
- $id = $request[$i]['Id'];
- $check_id = LegalEntities::find($id);
- if($check_id != null) {
- $data = LegalEntities::find($id);
- $find_director = Employees::find($request[$i]['DirectorId']);
- $find_commisioneer = Employees::find($request[$i]['CommissionerId']);
- if($request[$i]['Name'] != '' || $request[$i]['Name'] != null) {
- $data->Name = $request[$i]['Name'];
- } else {
- throw new Exception('Kolom Kosong / tidak valid.');
- }
- if($request[$i]['IsCentral'] == 'Ya') {
- $data->IsCentral = 1;
- } else if($request[$i]['IsCentral'] == 'Tidak') {
- $data->IsCentral = 0;
- } else {
- throw new Exception('Kolom Kosong / tidak valid.');
- }
- if($find_director != null) {
- $data->DirectorId = $request[$i]['DirectorId'];
- } else if($find_director == null) {
- $data->DirectorId = null;
- }
- if($find_commisioneer != null) {
- $data->CommissionerId = $request[$i]['CommissionerId'];
- } else if($find_commisioneer == null) {
- $data->CommissionerId = null;
- }
- $data->save();
- } else if($check_id == null) {
- $data = new LegalEntities();
- $find_director = Employees::find($request[$i]['DirectorId']);
- $find_commisioneer = Employees::find($request[$i]['CommissionerId']);
- if($request[$i]['Name'] != '' || $request[$i]['Name'] != null) {
- $data->Name = $request[$i]['Name'];
- } else {
- throw new Exception('Kolom Kosong / tidak valid.');
- }
- if($request[$i]['IsCentral'] == 'Ya') {
- $data->IsCentral = 1;
- } else if($request[$i]['IsCentral'] == 'Tidak') {
- $data->IsCentral = 0;
- } else {
- throw new Exception('Kolom Kosong / tidak valid.');
- }
- if($find_director != null) {
- $data->DirectorId = $request[$i]['DirectorId'];
- } else if($find_director == null) {
- $data->DirectorId = null;
- }
- if($find_commisioneer != null) {
- $data->CommissionerId = $request[$i]['CommissionerId'];
- } else if($find_commisioneer == null) {
- $data->CommissionerId = null;
- }
- $data->save();
- }
- $inserted_data = $inserted_data + 1;
- }
- }
- return response()->json(['message' => 'Data yang diinput sebanyak ' . $inserted_data]);
- } catch (Exception $e) {
- return response()->json(['message' => $exception->getMessage()]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement