Пример #1
0
 /**
  * Store an organisation
  * 1. store organisation
  * 2. store branch
  *
  * @param organisation
  * @return Response
  */
 public function store()
 {
     if (!Input::has('organisation')) {
         return new JSend('error', (array) Input::all(), 'Tidak ada data Organisation.');
     }
     $errors = new MessageBag();
     DB::beginTransaction();
     //1. Validate Organisation Parameter
     $organisation = Input::get('organisation');
     if (is_null($organisation['id'])) {
         $is_new = true;
     } else {
         $is_new = false;
     }
     $organisation_rules = ['name' => 'required|max:255', 'code' => 'required|max:255'];
     //1a. Get original data
     $organisation_data = \App\ThunderID\OrganisationManagementV1\Models\Organisation::findornew($organisation['id']);
     //1b. Validate Basic Organisation Parameter
     $validator = Validator::make($organisation, $organisation_rules);
     if (!$validator->passes()) {
         $errors->add('Organisation', $validator->errors());
     } else {
         //if validator passed, save Organisation
         $organisation_data = $organisation_data->fill($organisation);
         if (!$organisation_data->save()) {
             $errors->add('Organisation', $organisation_data->getError());
         }
     }
     //End of validate Organisation
     if ($errors->count()) {
         DB::rollback();
         return new JSend('error', (array) Input::all(), $errors);
     }
     DB::commit();
     $final_organisation = \App\ThunderID\OrganisationManagementV1\Models\Organisation::id($organisation_data['id'])->with(['branches', 'policies'])->first()->toArray();
     return new JSend('success', (array) $final_organisation);
 }