app/Http/Controllers/DistrictController.php
/*
     * Save district office or head of district
     */
Save district office or head of district
Save district office or head of district
public function store(Request $request){
        try{
            $validator = Validator::make($request->all(), [
                'district_name' => 'required',
                'central_office_id' => 'required',
            ]);
            if( $validator->fails()){
                $validatorErrors = [];
                foreach($validator->messages()->getMessages() as $fieldName => $messages) {
                    foreach( $messages as $message){
                        $validatorErrors[$fieldName] = $message;
                    }
                }
                throw new Exception(implode(' ',$validatorErrors));
            }
            $districtOffice = new DistrictOffice();
            $districtOffice->district_name = $request->input('district_name');
            $districtOffice->central_office_id = $request->input('central_office_id');
            $districtOffice->district_details = $request->input('district_details');
            if(!$districtOffice->save())
                throw new Exception("Critical error when save district office data!");
            $where = ['district_office_id'=>$districtOffice->id,'role'=>2];
            $districtOffice->head = TermRelation::findMyAdmin($where);
            return ['success'=>true, 'message'=>'Cetnral office save success', 'data'=>$districtOffice];
        }catch (Exception $e){
            return ['success'=>false, 'message'=> $e->getMessage()];
        }
    }
public function store ( Request $ request ) { try { $ validator = Validator :: make ( $ request -> all ( ) , [ 'district_name' => 'required' , 'central_office_id' => 'required' , ] ) ; if ( $ validator -> fails ( ) ) { $ validatorErrors = [ ] ; foreach ( $ validator -> messages ( ) -> getMessages ( ) as $ fieldName => $ messages ) { foreach ( $ messages as $ message ) { $ validatorErrors [ $ fieldName ] = $ message ; } } throw new Exception ( implode ( ' ' , $ validatorErrors ) ) ; } $ districtOffice = new DistrictOffice ( ) ; $ districtOffice -> district_name = $ request -> input ( 'district_name' ) ; $ districtOffice -> central_office_id = $ request -> input ( 'central_office_id' ) ; $ districtOffice -> district_details = $ request -> input ( 'district_details' ) ; if ( ! $ districtOffice -> save ( ) ) throw new Exception ( " Critical error when save district office data! " ) ; $ where = [ 'district_office_id' => $ districtOffice -> id , 'role' => 2 ] ; $ districtOffice -> head = TermRelation :: findMyAdmin ( $ where ) ; return [ 'success' => true , 'message' => 'Cetnral office save success' , 'data' => $ districtOffice ] ; } catch ( Exception $ e ) { return [ 'success' => false , 'message' => $ e -> getMessage ( ) ] ; } }
Save district office or head of district
Save district office or head of district