From c1176c3f688b61666132c9cc36dcc6d911275bb2 Mon Sep 17 00:00:00 2001 From: hustnzj <79649301@qq.com> Date: Wed, 12 Dec 2018 12:21:07 +0800 Subject: [PATCH] add a region config file so that you can change the default database table name --- src/Area.php | 6 ++++++ src/RegionServiceProvider.php | 1 + src/publishable/config/region.php | 11 +++++++++++ .../2018_01_01_000000_create_regions_table.php | 10 +++++----- 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 src/publishable/config/region.php diff --git a/src/Area.php b/src/Area.php index 5c8ff37..8b6a15a 100644 --- a/src/Area.php +++ b/src/Area.php @@ -8,6 +8,12 @@ class Area extends Model { + public function __construct() + { + parent::__construct(); + $this::setTable(config('region.table')); + } + public function parent() { return $this->belongsTo(Area::class, 'parent_id', 'id'); diff --git a/src/RegionServiceProvider.php b/src/RegionServiceProvider.php index 798d080..336b565 100644 --- a/src/RegionServiceProvider.php +++ b/src/RegionServiceProvider.php @@ -12,6 +12,7 @@ class RegionServiceProvider extends ServiceProvider public function register() { $this->publishes([__DIR__ . '/publishable/database/' => database_path()]); + $this->publishes([__DIR__ . '/publishable/config/' => config_path()]); } } \ No newline at end of file diff --git a/src/publishable/config/region.php b/src/publishable/config/region.php new file mode 100644 index 0000000..5297bf0 --- /dev/null +++ b/src/publishable/config/region.php @@ -0,0 +1,11 @@ + 'areas', +]; \ No newline at end of file diff --git a/src/publishable/database/migrations/2018_01_01_000000_create_regions_table.php b/src/publishable/database/migrations/2018_01_01_000000_create_regions_table.php index bf67a8e..5a34772 100644 --- a/src/publishable/database/migrations/2018_01_01_000000_create_regions_table.php +++ b/src/publishable/database/migrations/2018_01_01_000000_create_regions_table.php @@ -12,7 +12,7 @@ class CreateRegionsTable extends Migration public function up() { - Schema::create('areas', function (Blueprint $table) { + Schema::create(config('region.table'), function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('parent_id')->nullable()->index(); $table->string('name'); @@ -25,13 +25,13 @@ public function up() $provinces = $region->getRegionsWithCode(); foreach ($provinces as $province) { - $provinceId = DB::table('areas')->insertGetId([ + $provinceId = DB::table(config('region.table'))->insertGetId([ 'name' => $province['title'], 'code' => $province['ad_code'], 'type' => Region::PROVINCE, ]); foreach ($province['child'] as $city) { - $cityId = DB::table('areas')->insertGetId([ + $cityId = DB::table(config('region.table'))->insertGetId([ 'name' => $city['title'], 'parent_id' => $provinceId, 'code' => $city['ad_code'], @@ -40,14 +40,14 @@ public function up() $areas = array_map(function ($area) use ($cityId) { return ['name' => $area['title'], 'code' => $area['ad_code'], 'parent_id' => $cityId, 'type' => Region::AREA]; }, $city['child']); - DB::table('areas')->insert($areas); + DB::table(config('region.table'))->insert($areas); } } } public function down() { - Schema::dropIfExists('areas'); + Schema::dropIfExists(config('region.table')); } } \ No newline at end of file