/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     // validate request
     $validateDistrict = Validator::make($request->get('District'), District::$rules);
     $validationMessages = [];
     foreach ($request->get('DistrictTranslation') as $key => $value) {
         $validateDistrictTranslation = Validator::make($value, DistrictTranslation::$rules);
         if ($validateDistrictTranslation->fails()) {
             $validationMessages = array_merge_recursive($validationMessages, $validateDistrictTranslation->messages()->toArray());
         }
     }
     if ($validateDistrict->fails() or count($validationMessages) > 0) {
         $validationMessages = array_merge_recursive($validateDistrict->messages()->toArray(), $validationMessages);
         return redirect()->back()->withErrors($validationMessages)->withInput();
     }
     // get all languages
     $languages = Language::all();
     // find language default
     $languageDefault = $languages->where('is_key_language', 1)->first();
     if (is_null($languageDefault)) {
         $languageDefault = $languages->first();
     }
     // sure execute success, if not success rollback
     DB::transaction(function () use($request, $languageDefault) {
         $user = $request->user();
         // insert District
         $district = new District();
         $district->key = Common::createKeyURL($request->input('DistrictTranslation.' . $languageDefault->code . '.name'));
         $district->province_id = $request->input('District.province_id');
         $district->priority = $request->input('District.priority');
         $district->is_publish = $request->input('District.is_publish');
         $district->created_by = $user->name;
         $district->updated_by = $user->name;
         $district->save();
         // save data languages
         foreach ($request->get('DistrictTranslation') as $locale => $value) {
             $district->translateOrNew($locale)->name = $request->input('DistrictTranslation.' . $locale . '.name');
         }
         $district->save();
     });
     return redirect()->route('admin.districts.index');
 }