dr24's avatar
Level 2

Need help on finding appropriate Laravel seeder for German cities and states

I need help on finding right package, or database seeder for German cities, that have appropriate states linked to them and also with postal code values. Does someone now any, or is there some other way to implement that in my laravel application? Any help is appreciated.

0 likes
8 replies
MichalOravec's avatar

@dr24 You can get your data, it's just example.

use Illuminate\Support\Facades\Http;

$response = Http::get('https://raw.githubusercontent.com/zauberware/postal-codes-json-xml-csv/master/data/DE/zipcodes.de.json');

foreach($response->json() as $row) {
    YourModel::create([
        'zipcode' => $row['zipcode'],
        'place' => $row['place'],
        ...
        ...
    ]);
  	
    // all fields
    // echo $row['country_code'];
    // echo $row['zipcode'];
    // echo $row['place'];
    // echo $row['state'];
    // echo $row['state_code'];
    // echo $row['province'];
    // echo $row['province_code'];
    // echo $row['community'];
    // echo $row['community_code'];
    // echo $row['latitude'];
    // echo $row['longitude'];
}

Maybe you can create your array of array with data and then use https://laravel.com/docs/7.x/queries#inserts It's up to you, I hope you get main idea.

Of course in the newest version you can use $response->object() instead of $response->json()and then you can get your data like $row->zipcode;

1 like
dr24's avatar
Level 2

@michaloravec

Ok so I made all this as it should, but I get Class 'Illuminate\Support\Facades\Http' not found . I already have Guzzle HTTP client installed through composer composer require guzzlehttp/guzzle and my laravel version is Laravel Framework 6.18.3. It won't import/see Http class when. Can you help?

MichalOravec's avatar
Level 75

@dr24 This Illuminate\Support\Facades\Http is new Laravel 7, so just use Guzzle instead of that.

use GuzzleHttp\Client;

$response = (new Client())->request('GET', 'https://raw.githubusercontent.com/zauberware/postal-codes-json-xml-csv/master/data/DE/zipcodes.de.json')->getBody();

$json = json_decode($response, true);

foreach($json  as $row) {
    YourModel::create([
        'zipcode' => $row['zipcode'],
        'place' => $row['place'],
        ...
        ...
    ]);
  	
    // all fields
    // echo $row['country_code'];
    // echo $row['zipcode'];
    // echo $row['place'];
    // echo $row['state'];
    // echo $row['state_code'];
    // echo $row['province'];
    // echo $row['province_code'];
    // echo $row['community'];
    // echo $row['community_code'];
    // echo $row['latitude'];
    // echo $row['longitude'];
}
1 like
dr24's avatar
Level 2

@michaloravec

Yes, it works now! Thank you very much. One more thing if you know, can I somehow update this code to seed faster when I use php artisan migrate:fresh --seed? Currently it takes about minute, which can be long because I often use that command

MichalOravec's avatar

@dr24 There are a lot of data. Maybe just run this seeder once, so remove it after from your DatabaseSeeder class.

Please or to participate in this conversation.