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

Custom tables #80

Closed
wants to merge 8 commits into from
Closed
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
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ return [

/*
|--------------------------------------------------------------------------
| Experience Table
| Table Names
|--------------------------------------------------------------------------
|
| This value is the name of the table that will be used to store experience data.
| These values are the names of the tables that will be used to store the data.
|
*/
'table' => 'experiences',
'tables' => [
'experiences' => 'experiences',
'experience_audits' => 'experience_audits',
'achievements' => 'achievements',
'levels' => 'levels',
'streaks' => 'streaks',
'streak_histories' => 'streak_histories',
'streak_activities' => 'streak_activities',
],

/*
|-----------------------------------------------------------------------
Expand Down
23 changes: 20 additions & 3 deletions config/level-up.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,30 @@

/*
|--------------------------------------------------------------------------
| Experience Table
| Table Names
|--------------------------------------------------------------------------
|
| This value is the name of the table that will be used to store experience data.
| This value is the name of the tables that will be used to store level up data.
| You can change the table names to fit your application.
|
| The default table names are using Laravel conventions.
|
| It is recommended to keep the table names as they are, only change them if
| you know what you are doing.
|
| example: 'typical_table_name' => 'new_table_name',
|
*/
'table' => 'experiences',
'tables' => [
'achievement_user' => 'achievement_user', // pivot table
'experiences' => 'experiences',
'experience_audits' => 'experience_audits',
'achievements' => 'achievements',
'levels' => 'levels',
'streaks' => 'streaks',
'streak_histories' => 'streak_histories',
'streak_activities' => 'streak_activities',
],

/*
|-----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::table('streaks', function (Blueprint $table) {
Schema::table(config('level-up.tables.streaks'), function (Blueprint $table) {
$table->after('activity_at', function (Blueprint $table) {
$table->timestamp('frozen_until')->nullable();
});
Expand All @@ -16,7 +16,7 @@ return new class extends Migration {

public function down(): void
{
Schema::table('streaks', function (Blueprint $table) {
Schema::table(config('level-up.tables.streaks'), function (Blueprint $table) {
$table->dropColumn('frozen_until');
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('achievement_user', function (Blueprint $table) {
Schema::create(config('level-up.tables.achievement_user'), function (Blueprint $table) {
$table->id();
$table->foreignId(column: config('level-up.user.foreign_key'))->constrained(config('level-up.user.users_table'));
$table->foreignId(column: 'achievement_id')->constrained();
$table->foreignId(column: 'achievement_id')->constrained(config('level-up.tables.achievements'));
$table->integer(column: 'progress')->nullable()->index();
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('achievement_user');
Schema::dropIfExists(config('level-up.tables.achievements') . '_' . config('level-up.user.users_table'));
}
};
4 changes: 2 additions & 2 deletions database/migrations/create_achievements_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('achievements', function (Blueprint $table) {
Schema::create(config('level-up.tables.achievements'), function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('is_secret')->default(false);
Expand All @@ -19,6 +19,6 @@ return new class extends Migration {

public function down(): void
{
Schema::dropIfExists('achievements');
Schema::dropIfExists(config('level-up.tables.achievements'));
}
};
4 changes: 2 additions & 2 deletions database/migrations/create_experience_audits_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('experience_audits', function (Blueprint $table) {
Schema::create(config('level-up.tables.experience_audits'), function (Blueprint $table) {
$table->id();
$table->foreignId(config('level-up.user.foreign_key'))->constrained(config('level-up.user.users_table'));
$table->integer('points')->index();
Expand All @@ -21,6 +21,6 @@ return new class extends Migration {

public function down(): void
{
Schema::dropIfExists('experience_audits');
Schema::dropIfExists(config('level-up.tables.experience_audits'));
}
};
4 changes: 2 additions & 2 deletions database/migrations/create_experiences_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ return new class extends Migration
{
public function up()
{
Schema::create(config('level-up.table'), function (Blueprint $table) {
Schema::create(config('level-up.tables.experiences'), function (Blueprint $table) {
$table->id();
$table->foreignId(config('level-up.user.foreign_key'))->constrained(config('level-up.user.users_table'));
$table->foreignId('level_id')->constrained();
Expand All @@ -19,6 +19,6 @@ return new class extends Migration

public function down()
{
Schema::dropIfExists(config('level-up.table'));
Schema::dropIfExists(config('level-up.tables.experiences'));
}
};
4 changes: 2 additions & 2 deletions database/migrations/create_levels_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ return new class extends Migration
{
public function up()
{
Schema::create('levels', function (Blueprint $table) {
Schema::create(config('level-up.tables.levels'), function (Blueprint $table) {
$table->id();
$table->integer('level')->unique();
$table->integer('next_level_experience')->nullable()->index();
Expand All @@ -18,6 +18,6 @@ return new class extends Migration

public function down()
{
Schema::dropIfExists('levels');
Schema::dropIfExists(config('level-up.tables.levels'));
}
};
4 changes: 2 additions & 2 deletions database/migrations/create_streak_activities_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('streak_activities', function (Blueprint $table) {
Schema::create(config('level-up.tables.streak_activities'), function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->text('description')->nullable();
Expand All @@ -17,6 +17,6 @@ return new class extends Migration {

public function down(): void
{
Schema::dropIfExists('streak_activities');
Schema::dropIfExists(config('level-up.tables.streak_activities'));
}
};
4 changes: 2 additions & 2 deletions database/migrations/create_streak_histories_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ return new class extends Migration
{
public function up(): void
{
Schema::create(table: 'streak_histories', callback: function (Blueprint $table) {
Schema::create(config('level-up.tables.streak_histories'), function (Blueprint $table) {
$table->id();
$table->foreignId(column: config(key: 'level-up.user.foreign_key'))->constrained(table: config(key: 'level-up.user.users_table'))->cascadeOnDelete();
$table->foreignIdFor(model: Activity::class)->constrained(table: 'streak_activities');
Expand All @@ -22,6 +22,6 @@ return new class extends Migration

public function down(): void
{
Schema::dropIfExists('streak_histories');
Schema::dropIfExists(config('level-up.tables.streak_histories'));
}
};
12 changes: 6 additions & 6 deletions database/migrations/create_streaks_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('streaks', function (Blueprint $table) {
Schema::create(config('level-up.tables.streaks'), function (Blueprint $table) {
$table->id();
$table->foreignId(column: config('level-up.user.foreign_key'))->constrained()->onDelete('cascade');
$table->foreignId(column: 'activity_id')->constrained('streak_activities')->onDelete('cascade');
$table->integer(column: 'count')->default(1);
$table->timestamp(column: 'activity_at');
$table->foreignId(config('level-up.user.foreign_key'))->constrained()->onDelete('cascade');
$table->foreignId('activity_id')->constrained(config('level-up.tables.streak_activities'))->onDelete('cascade');
$table->integer('count')->default(1);
$table->timestamp('activity_at');
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('streaks');
Schema::dropIfExists(config('level-up.tables.streaks'));
}
};
6 changes: 6 additions & 0 deletions src/Models/Achievement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class Achievement extends Model

protected $guarded = [];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config('level-up.tables.achievements') ?: parent::getTable();
}

public function users(): BelongsToMany
{
return $this->belongsToMany(related: config(key: 'level-up.user.model'));
Expand Down
6 changes: 5 additions & 1 deletion src/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ class Activity extends Model
{
use HasFactory;

protected $table = 'streak_activities';
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config('level-up.tables.streak_activities') ?: parent::getTable();
}

protected $guarded = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Models/Experience.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Experience extends Model
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config(key: 'level-up.table');
$this->table = config(key: 'level-up.tables.experiences');
}

protected $guarded = [];
Expand Down
6 changes: 6 additions & 0 deletions src/Models/ExperienceAudit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class ExperienceAudit extends Model
'type' => AuditType::class,
];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config(key: 'level-up.tables.experience_audits');
}

public function user(): BelongsTo
{
return $this->belongsTo(config(key: 'level-up.user.model'));
Expand Down
6 changes: 6 additions & 0 deletions src/Models/Level.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class Level extends Model
{
protected $guarded = [];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config(key: 'level-up.tables.levels');
}

/**
* @throws \LevelUp\Experience\Exceptions\LevelExistsException
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Models/Pivots/AchievementUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

class AchievementUser extends Pivot
{
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config('level-up.tables.achievement_user');
}

public function scopeWithProgress(Builder $query, int $progress): Collection
{
return $query->where(column: 'progress', operator: $progress)->get();
Expand Down
6 changes: 6 additions & 0 deletions src/Models/Streak.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class Streak extends Model
'frozen_until' => 'datetime',
];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config(key: 'level-up.tables.streaks');
}

public function user(): BelongsTo
{
return $this->belongsTo(config(key: 'level-up.user.model'));
Expand Down
6 changes: 6 additions & 0 deletions src/Models/StreakHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ class StreakHistory extends Model
'started_at' => 'datetime',
'ended_at' => 'datetime',
];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config(key: 'level-up.tables.streak_history');
}
}
1 change: 0 additions & 1 deletion tests/Concerns/HasStreaksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use LevelUp\Experience\Events\StreakStarted;
use LevelUp\Experience\Events\StreakUnfroze;
use LevelUp\Experience\Models\Activity;

use function Pest\Laravel\travel;

uses()->group('streaks');
Expand Down
Loading