Skip to content

Commit 2ed09b1

Browse files
amandiobmjamesmills
authored andcommitted
Publishing migration dynamically (jamesmills#7)
* Publishing migration dynamically * Fixed migration order * Keep it simple * Updated Readme.md * Use clean up
1 parent 14077fb commit 2ed09b1

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ Pull in the package using Composer
3737
composer require jamesmills/laravel-timezone
3838
```
3939

40+
Publish database migrations
41+
42+
```
43+
php artisan vendor:publish --provider="JamesMills\LaravelTimezone\LaravelTimezoneServiceProvider" --tag=migrations
44+
```
45+
4046
Run the database migrations. This will add a `timezone` column to your `users` table.
4147

4248
```

src/LaravelTimezoneServiceProvider.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace JamesMills\LaravelTimezone;
44

5+
use Illuminate\Foundation\AliasLoader;
56
use Illuminate\Support\Facades\Blade;
67
use Illuminate\Support\Facades\Event;
78
use Illuminate\Support\ServiceProvider;
8-
use Illuminate\Foundation\AliasLoader;
99
use JamesMills\LaravelTimezone\Listeners\Auth\UpdateUsersTimezone;
1010

1111
class LaravelTimezoneServiceProvider extends ServiceProvider
@@ -25,8 +25,13 @@ class LaravelTimezoneServiceProvider extends ServiceProvider
2525
*/
2626
public function boot()
2727
{
28-
// Register database migrations
29-
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
28+
// Allow migrations publish
29+
if (! class_exists('AddTimezoneColumnToUsersTable')) {
30+
$timestamp = date('Y_m_d_His', time());
31+
$this->publishes([
32+
__DIR__.'/database/migrations/add_timezone_column_to_users_table.php.stub' => database_path("/migrations/{$timestamp}_add_timezone_column_to_users_table.php"),
33+
], 'migrations');
34+
}
3035

3136
// Register the Timezone alias
3237
AliasLoader::getInstance()->alias('Timezone', \JamesMills\LaravelTimezone\Facades\Timezone::class);
@@ -36,7 +41,7 @@ public function boot()
3641

3742
// Allow config publish
3843
$this->publishes([
39-
__DIR__ . '/config/timezone.php' => config_path('timezone.php'),
44+
__DIR__.'/config/timezone.php' => config_path('timezone.php'),
4045
], 'config');
4146

4247
// Register a blade directive to show user date/time in their timezone
@@ -69,4 +74,5 @@ public function register()
6974
{
7075
$this->app->bind('timezone', Timezone::class);
7176
}
77+
7278
}

src/database/migrations/2016_01_01_193651_add_timezone_column_to_users_table_package.php src/database/migrations/add_timezone_column_to_users_table.php.stub

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
55

6-
class AddTimezoneColumnToUsersTablePackage extends Migration
6+
class AddTimezoneColumnToUsersTable extends Migration
77
{
88
/**
99
* Run the migrations.

0 commit comments

Comments
 (0)