app/Containers/Import/UI/CLI/Commands/ImportNewDataCommand.php
/**
     * Execute the console command.
     *
     * @return void
     */
Execute the console command.
@return void
Execute the console command . @return void
public function handle()
    {
        $this->info('Importing...');
        DB::beginTransaction();
        try {
            $this->info('Importing farmer...');
            $project = Project::find(1);
            $farmers = Import20190731::all();
            $this->output->progressStart(count($farmers));
            foreach ($farmers as $entity)
            {
                $farmer = Apiato::call('Farmer@CreateFarmerTask', [[
                    'name'          => $entity->name,
                    'import_id'     => $this->importId,
                    'project_id'    => $project->id,
                ]]);
                if (!$village = Village::where('name', $entity->village)->whereHas('district', function($q) use ($entity){
                    $q->where('name', $entity->district);
                })->first()) {
                    $district = District::firstOrCreate([
                        'name' => $entity->district,
                    ]);
                    $village = Apiato::call('Village@CreateVillageTask', [[
                        'name' => $entity->village,
                        'district_id'   => $district->id,
                    ]]);
                }
                $polygon = Apiato::call('Geo@CreatePolygonTask', [$entity->geom]);
                $plot = Apiato::call('Plot@CreatePlotTask', [[
                    'farmer_id'     => $farmer->id,
                    'village_id'    => optional($village)->id,
                    'polygon_id'    => optional($polygon)->id,
                    'planting_date' => $entity->plantingda,
                    'import_id'     => $this->importId,
                ]]);
                $this->output->progressAdvance();
            }
            $this->output->progressFinish();
            DB::commit();
        } catch (Exception $e) {
            $this->console->writeln('Error! Something went wrong!');
            $this->console->writeln('Error: ' . $e);
            DB::rollBack();
            exit;
        }
        $this->info('Importing finished');
    }
public function handle ( ) { $ this -> info ( 'Importing...' ) ; DB :: beginTransaction ( ) ; try { $ this -> info ( 'Importing farmer...' ) ; $ project = Project :: find ( 1 ) ; $ farmers = Import20190731 :: all ( ) ; $ this -> output -> progressStart ( count ( $ farmers ) ) ; foreach ( $ farmers as $ entity ) { $ farmer = Apiato :: call ( 'Farmer@CreateFarmerTask' , [ [ 'name' => $ entity -> name , 'import_id' => $ this -> importId , 'project_id' => $ project -> id , ] ] ) ; if ( ! $ village = Village :: where ( 'name' , $ entity -> village ) -> whereHas ( 'district' , function ( $ q ) use ( $ entity ) { $ q -> where ( 'name' , $ entity -> district ) ; } ) -> first ( ) ) { $ district = District :: firstOrCreate ( [ 'name' => $ entity -> district , ] ) ; $ village = Apiato :: call ( 'Village@CreateVillageTask' , [ [ 'name' => $ entity -> village , 'district_id' => $ district -> id , ] ] ) ; } $ polygon = Apiato :: call ( 'Geo@CreatePolygonTask' , [ $ entity -> geom ] ) ; $ plot = Apiato :: call ( 'Plot@CreatePlotTask' , [ [ 'farmer_id' => $ farmer -> id , 'village_id' => optional ( $ village ) -> id , 'polygon_id' => optional ( $ polygon ) -> id , 'planting_date' => $ entity -> plantingda , 'import_id' => $ this -> importId , ] ] ) ; $ this -> output -> progressAdvance ( ) ; } $ this -> output -> progressFinish ( ) ; DB :: commit ( ) ; } catch ( Exception $ e ) { $ this -> console -> writeln ( 'Error! Something went wrong!' ) ; $ this -> console -> writeln ( 'Error: ' . $ e ) ; DB :: rollBack ( ) ; exit ; } $ this -> info ( 'Importing finished' ) ; }
Execute the console command.
Execute the console command .
//take the first project for now, as there is only one //create farmer // village //add polygon //create plot