app/Http/Controllers/CompanyController.php
/*
     * Save company office or head of district
     */
Save company office or head of district
Save company office or head of district
public function store(Request $request){
        try{
            $validator = Validator::make($request->all(), [
                'company_name' => 'required',
                'central_office_id' => 'required',
                'formation_office_id' => 'required',
                'unit_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));
            }
            $Company = new Company();
            $Company->company_name = $request->input('company_name');
            $Company->central_office_id = $request->input('central_office_id');
            $Company->district_office_id = $request->input('formation_office_id');
            $Company->unit_id = $request->input('unit_id');
            $Company->company_details = $request->input('company_details');
            if(!$Company->save())
                throw new Exception("Critical error when save company data!");
            $where = ['company_id'=>$Company->id,'role'=>4];
            $Company->head = TermRelation::findMyAdmin($where);
            return ['success'=>true, 'message'=>'Company save success', 'data'=>$Company];
        }catch (Exception $e){
            return ['success'=>false, 'message'=> $e->getMessage()];
        }
    }
public function store ( Request $ request ) { try { $ validator = Validator :: make ( $ request -> all ( ) , [ 'company_name' => 'required' , 'central_office_id' => 'required' , 'formation_office_id' => 'required' , 'unit_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 ) ) ; } $ Company = new Company ( ) ; $ Company -> company_name = $ request -> input ( 'company_name' ) ; $ Company -> central_office_id = $ request -> input ( 'central_office_id' ) ; $ Company -> district_office_id = $ request -> input ( 'formation_office_id' ) ; $ Company -> unit_id = $ request -> input ( 'unit_id' ) ; $ Company -> company_details = $ request -> input ( 'company_details' ) ; if ( ! $ Company -> save ( ) ) throw new Exception ( " Critical error when save company data! " ) ; $ where = [ 'company_id' => $ Company -> id , 'role' => 4 ] ; $ Company -> head = TermRelation :: findMyAdmin ( $ where ) ; return [ 'success' => true , 'message' => 'Company save success' , 'data' => $ Company ] ; } catch ( Exception $ e ) { return [ 'success' => false , 'message' => $ e -> getMessage ( ) ] ; } }
Save company office or head of district
Save company office or head of district