Skip to content

Commit

Permalink
laravel 8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
dipeshsukhia committed Apr 25, 2021
1 parent f54b8e7 commit 2dc3290
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Quality Score](https://img.shields.io/scrutinizer/g/dipeshsukhia/laravel-country-state-city-data.svg?style=flat-square)](https://scrutinizer-ci.com/g/dipeshsukhia/laravel-country-state-city-data)
[![Total Downloads](https://img.shields.io/packagist/dt/dipeshsukhia/laravel-country-state-city-data.svg?style=flat-square)](https://packagist.org/packages/dipeshsukhia/laravel-country-state-city-data)

World`s Country State City Provider for Laravel 5.x, 6.x, 7.x
World`s Country State City Provider for Laravel 5.x, 6.x, 7.x, 8.x

## Installation

Expand Down
63 changes: 37 additions & 26 deletions src/LaravelCountryStateCityDataServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class LaravelCountryStateCityDataServiceProvider extends ServiceProvider
{
const STUB_DIR = __DIR__.'/resources/stubs/';
/**
* Bootstrap the application services.
*/
Expand All @@ -14,38 +15,43 @@ public function boot()
/*
* Optional methods to load your package assets
*/
// $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'laravel-country-state-city-data');
// $this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-country-state-city-data');
// $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
// $this->loadRoutesFrom(__DIR__.'/routes.php');

if ($this->app->runningInConsole()) {
/* model */
if(is_dir(base_path('app/Models'))){
$modelDir = base_path('app/Models');
$modelNameSpace = "\\Models";
}else{
$modelDir = base_path('app');
$modelNameSpace = "";
}

$this->publishes([
__DIR__.'/migrations' => database_path('migrations'),
__DIR__.'/Models' => app_path(),
__DIR__.'/DataProviders' => app_path('DataProviders'),
__DIR__.'/seeds' => database_path('seeds'),
], 'LaravelCountryStateCityData');
foreach (['Country','State','City'] as $modelName){
$ModelTemplate = self::getStubContents("Models/{$modelName}.stub");
$ModelTemplate = str_replace('{{modelNameSpace}}', $modelNameSpace, $ModelTemplate);
file_put_contents($modelDir."/{$modelName}.php", $ModelTemplate);
}

/*$this->publishes([
__DIR__.'/../config/config.php' => config_path('laravel-country-state-city-data.php'),
], 'config');*/
/* model */

// Publishing the views.
/*$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/laravel-country-state-city-data'),
], 'views');*/
/* seeders */
if(is_dir(database_path('seeds'))){
$seedDir = database_path('seeds');
$seederNameSpace = "Seeder";
}else{
$seedDir = database_path('seeders');
$seederNameSpace = "Seeders";
}
$seederTemplate = self::getStubContents('CountryStateCityTableSeeder.stub');
$seederTemplate = str_replace('{{seederNameSpace}}', $seederNameSpace, $seederTemplate);
$seederTemplate = str_replace('{{modelNameSpace}}', $modelNameSpace, $seederTemplate);
file_put_contents($seedDir.'/CountryStateCityTableSeeder.php', $seederTemplate);
/* seeders */

// Publishing assets.
/*$this->publishes([
__DIR__.'/../resources/assets' => public_path('vendor/laravel-country-state-city-data'),
], 'assets');*/

// Publishing the translation files.
/*$this->publishes([
__DIR__.'/../resources/lang' => resource_path('lang/vendor/laravel-country-state-city-data'),
], 'lang');*/
$this->publishes([
__DIR__.'/resources/migrations' => database_path('migrations'),
__DIR__.'/resources/DataProviders' => app_path('DataProviders'),
], 'LaravelCountryStateCityData');

// Registering package commands.
// $this->commands([]);
Expand All @@ -65,4 +71,9 @@ public function register()
return new LaravelCountryStateCityData;
});*/
}

private function getStubContents($stubName)
{
return file_get_contents(self::STUB_DIR.$stubName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CreateCountryStateCityTable extends Migration
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->increments('id');
$table->bigIncrements('id');
$table->string('name',255);
$table->enum('status', ['active', 'inactive'])->default('active');
$table->timestamp('created_at')->useCurrent();
Expand All @@ -24,8 +24,8 @@ public function up()
});

Schema::create('states', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('country_id');
$table->bigIncrements('id');
$table->unsignedBigInteger('country_id');
$table->string('name',255);
$table->enum('status', ['active', 'inactive'])->default('active');
$table->timestamp('created_at')->useCurrent();
Expand All @@ -37,8 +37,8 @@ public function up()
});

Schema::create('cities', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('state_id');
$table->bigIncrements('id');
$table->unsignedBigInteger('state_id');
$table->string('name',255);
$table->enum('status', ['active', 'inactive'])->default('active');
$table->timestamp('created_at')->useCurrent();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace Database\{{seederNameSpace}};

use Illuminate\Database\Seeder;
use App\Country;
use App\State;
use App\City;
use App{{modelNameSpace}}\Country;
use App{{modelNameSpace}}\State;
use App{{modelNameSpace}}\City;
use App\DataProviders\CountryStateCityDataProvider;


class CountryStateCityTableSeeder extends Seeder
{
/**
Expand All @@ -17,11 +17,9 @@ class CountryStateCityTableSeeder extends Seeder
public function run()
{
Country::insert(CountryStateCityDataProvider::Countries());

State::insert(CountryStateCityDataProvider::States());

foreach (collect(CountryStateCityDataProvider::Cities())->chunk(15000) as $chunkCities){
City::insert($chunkCities->toArray());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App;
namespace App{{modelNameSpace}};

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App;
namespace App{{modelNameSpace}};

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App;
namespace App{{modelNameSpace}};

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down

0 comments on commit 2dc3290

Please sign in to comment.