Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a region config file so that you can change the default table #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Area.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions src/RegionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
}

}
11 changes: 11 additions & 0 deletions src/publishable/config/region.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Created by PhpStorm.
* User: hustnzj
* Date: 2018/12/12
* Time: 12:17 PM
*/

return [
'table' => 'areas',
];
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'],
Expand All @@ -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'));
}

}