app/Console/Commands/Setup/ImportStates.php
/**
     * Execute the console command.
     *
     * @return mixed
     */
Execute the console command.
@return mixed
Execute the console command . @return mixed
public function handle()
    {
        $this->info('Importing entries into the database...');
        try {
            $filename = resource_path('country-states.json');
            if (!file_exists($filename)) {
                throw new FileNotFoundException('Could not find the country-states.json file at: '.$filename);
            }
            if (!is_readable($filename)) {
                throw new FileException('The country-states.json ('.$filename.') file is not readable by the process.');
            }
            $entries = json_decode(file_get_contents($filename), true);
            foreach ($entries['countries'] as $entry) {
                $this->line('Setting up states for country: '.$entry['country'].'...');
                $country = Country::where('name', 'like', $entry['country'])->first();
                if (empty($country)) {
                    $this->warn('Could not find country...skipping...');
                    continue;
                }
                $createdStates = $country->states()->pluck('name')->all();
                $states = [];
                $this->info('States for '.$country->name . ' [Current: '.count($createdStates).']');
                foreach ($entry['states'] as $state) {
                    $this->line('Processing: '.$state);
                    if (in_array($state, $createdStates)) {
                        $this->line('already exists; skipping...');
                        continue;
                    }
                    $states[] = ['uuid' => Uuid::uuid1()->toString(), 'name' => $state];
                }
                $this->line('Adding '.count($states).' states to country...');
                $country->states()->createMany($states);
                $this->line('saved.');
            }
            $this->info('Done!');
        } catch (\Throwable $e) {
            $this->error($e->getMessage());
        }
        return;
    }
public function handle ( ) { $ this -> info ( 'Importing entries into the database...' ) ; try { $ filename = resource_path ( 'country-states.json' ) ; if ( ! file_exists ( $ filename ) ) { throw new FileNotFoundException ( 'Could not find the country-states.json file at: ' . $ filename ) ; } if ( ! is_readable ( $ filename ) ) { throw new FileException ( 'The country-states.json (' . $ filename . ') file is not readable by the process.' ) ; } $ entries = json_decode ( file_get_contents ( $ filename ) , true ) ; foreach ( $ entries [ 'countries' ] as $ entry ) { $ this -> line ( 'Setting up states for country: ' . $ entry [ 'country' ] . '...' ) ; $ country = Country :: where ( 'name' , 'like' , $ entry [ 'country' ] ) -> first ( ) ; if ( empty ( $ country ) ) { $ this -> warn ( 'Could not find country...skipping...' ) ; continue ; } $ createdStates = $ country -> states ( ) -> pluck ( 'name' ) -> all ( ) ; $ states = [ ] ; $ this -> info ( 'States for ' . $ country -> name . ' [Current: ' . count ( $ createdStates ) . ']' ) ; foreach ( $ entry [ 'states' ] as $ state ) { $ this -> line ( 'Processing: ' . $ state ) ; if ( in_array ( $ state , $ createdStates ) ) { $ this -> line ( 'already exists; skipping...' ) ; continue ; } $ states [ ] = [ 'uuid' => Uuid :: uuid1 ( ) -> toString ( ) , 'name' => $ state ] ; } $ this -> line ( 'Adding ' . count ( $ states ) . ' states to country...' ) ; $ country -> states ( ) -> createMany ( $ states ) ; $ this -> line ( 'saved.' ) ; } $ this -> info ( 'Done!' ) ; } catch ( \ Throwable $ e ) { $ this -> error ( $ e -> getMessage ( ) ) ; } return ; }
Execute the console command.
Execute the console command .
# get the filename # read in the file data # to see if we already have the model # get the states # states to be created for the country # small information display # add the record