Skip to content

Commit

Permalink
update migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Nur Wachid committed Jun 26, 2024
1 parent 196fd61 commit 55c4b56
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ In Laravel 5.5 and higher versions, the service provider will automatically get
You must publish [the migration](https://github.com/turahe/laravel-counters/tree/master/database/migrations) with:

```bash
php artisan vendor:publish --provider="Turahe\Counters\CountersServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Turahe\Counters\CountersServiceProvider"
```

After the migration has been published you can create the tables by running the migrations:
Expand Down
23 changes: 22 additions & 1 deletion src/CountersServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Turahe\Counters;

use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
use Turahe\Counters\Facades\Counters;
use Illuminate\Support\ServiceProvider;

Expand All @@ -14,9 +16,13 @@ class CountersServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
__DIR__.'/../database/migrations/0000_00_00_000000_create_counters_tables.php' => $this->app->databasePath().'/migrations/0000_00_00_000000_create_counters_tables.php',
__DIR__.'/../database/migrations/0000_00_00_000000_create_counters_tables.php' => $this->getMigrationFileName('create_permission_tables.php'),
], 'migrations');

$this->publishes([
__DIR__.'/../config/counter.php' => $this->app->configPath('counter.php'),
], 'config');

if ($this->app instanceof \Illuminate\Foundation\Application) {
$databasePath = __DIR__ . '/../database/migrations';
$this->loadMigrationsFrom($databasePath);
Expand All @@ -34,4 +40,19 @@ public function register()
return new Counters();
});
}

/**
* Returns existing migration file if found, else uses the current timestamp.
*/
protected function getMigrationFileName(string $migrationFileName): string
{
$timestamp = date('Y_m_d_His');

$filesystem = $this->app->make(Filesystem::class);

return Collection::make([$this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR])
->flatMap(fn ($path) => $filesystem->glob($path.'*_'.$migrationFileName))
->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}")
->first();
}
}
11 changes: 5 additions & 6 deletions src/Traits/HasCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ trait HasCounter
public function counters()
{
return $this->morphToMany(
Counter::class,
config('counter.models.counter'),
'counterable',
'counterables'
config('counter.models.table_pivot_name')
)->withPivot('value', 'id')
->withTimestamps();
}
Expand All @@ -31,7 +31,6 @@ public function counters()
*/
public function getCounter($key)
{
// dd($this->counters);
$counter = $this->counters->where('key', $key)->first();

//connect the counter to the object if it's not exist
Expand All @@ -55,13 +54,13 @@ public function hasCounter($key)

/**
* @param $key
* @return null
* @return int
* Get the related model value of the counter for the given $key
*/
public function getCounterValue($key)
public function getCounterValue($key): int
{
$counter = $this->getCounter($key);
$value = null;
$value = 0;

if ($counter) {
$value = $counter->pivot->value;
Expand Down

0 comments on commit 55c4b56

Please sign in to comment.