Skip to content

Commit 6443f9a

Browse files
authored
Make model table names configurable (#165)
Add model table names configuration
1 parent ea6dc25 commit 6443f9a

9 files changed

+43
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to `laravel-love` will be documented in this file.
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- ([#165]) Added table names configuration
10+
711
### Fixed
812

913
- ([#161]) Removed redundant queues from Reactant listeners
@@ -472,6 +476,7 @@ Follow [upgrade instructions](UPGRADING.md#from-v5-to-v6) to migrate database to
472476
[1.1.1]: https://github.com/cybercog/laravel-love/compare/1.1.0...1.1.1
473477
[1.1.0]: https://github.com/cybercog/laravel-love/compare/1.0.0...1.1.0
474478

479+
[#165]: https://github.com/cybercog/laravel-love/pull/165
475480
[#161]: https://github.com/cybercog/laravel-love/pull/161
476481
[#158]: https://github.com/cybercog/laravel-love/pull/158
477482
[#151]: https://github.com/cybercog/laravel-love/pull/151

config/love.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
'storage' => [
2828
'database' => [
2929
'connection' => env('DB_CONNECTION', 'mysql'),
30+
'tables' => [
31+
'love_reacters' => null,
32+
'love_reactants' => null,
33+
'love_reaction_types' => null,
34+
'love_reactions' => null,
35+
'love_reactant_reaction_counters' => null,
36+
'love_reactant_reaction_totals' => null,
37+
],
3038
],
3139
],
3240

database/migrations/2018_07_22_000100_create_love_reacters_table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14+
use Cog\Laravel\Love\Reacter\Models\Reacter;
1415
use Cog\Laravel\Love\Support\Database\Migration;
1516
use Illuminate\Database\Schema\Blueprint;
1617

@@ -23,7 +24,7 @@ final class CreateLoveReactersTable extends Migration
2324
*/
2425
public function up(): void
2526
{
26-
$this->schema->create('love_reacters', function (Blueprint $table) {
27+
$this->schema->create((new Reacter())->getTable(), function (Blueprint $table) {
2728
$table->bigIncrements('id');
2829
$table->string('type');
2930
$table->timestamps();
@@ -39,6 +40,6 @@ public function up(): void
3940
*/
4041
public function down(): void
4142
{
42-
$this->schema->dropIfExists('love_reacters');
43+
$this->schema->dropIfExists((new Reacter())->getTable());
4344
}
4445
}

database/migrations/2018_07_22_001000_create_love_reactants_table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14+
use Cog\Laravel\Love\Reactant\Models\Reactant;
1415
use Cog\Laravel\Love\Support\Database\Migration;
1516
use Illuminate\Database\Schema\Blueprint;
1617

@@ -23,7 +24,7 @@ final class CreateLoveReactantsTable extends Migration
2324
*/
2425
public function up(): void
2526
{
26-
$this->schema->create('love_reactants', function (Blueprint $table) {
27+
$this->schema->create((new Reactant)->getTable(), function (Blueprint $table) {
2728
$table->bigIncrements('id');
2829
$table->string('type');
2930
$table->timestamps();
@@ -39,6 +40,6 @@ public function up(): void
3940
*/
4041
public function down(): void
4142
{
42-
$this->schema->dropIfExists('love_reactants');
43+
$this->schema->dropIfExists((new Reactant)->getTable());
4344
}
4445
}

database/migrations/2018_07_22_001500_create_love_reaction_types_table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14+
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
1415
use Cog\Laravel\Love\Support\Database\Migration;
1516
use Illuminate\Database\Schema\Blueprint;
1617

@@ -23,7 +24,7 @@ final class CreateLoveReactionTypesTable extends Migration
2324
*/
2425
public function up(): void
2526
{
26-
$this->schema->create('love_reaction_types', function (Blueprint $table) {
27+
$this->schema->create((new ReactionType())->getTable(), function (Blueprint $table) {
2728
$table->bigIncrements('id');
2829
$table->string('name');
2930
$table->tinyInteger('mass');
@@ -40,6 +41,6 @@ public function up(): void
4041
*/
4142
public function down(): void
4243
{
43-
$this->schema->dropIfExists('love_reaction_types');
44+
$this->schema->dropIfExists((new ReactionType())->getTable());
4445
}
4546
}

database/migrations/2018_07_22_002000_create_love_reactions_table.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
declare(strict_types=1);
1313

14+
use Cog\Laravel\Love\Reactant\Models\Reactant;
15+
use Cog\Laravel\Love\Reacter\Models\Reacter;
16+
use Cog\Laravel\Love\Reaction\Models\Reaction;
17+
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
1418
use Cog\Laravel\Love\Support\Database\Migration;
1519
use Illuminate\Database\Schema\Blueprint;
1620

@@ -23,7 +27,7 @@ final class CreateLoveReactionsTable extends Migration
2327
*/
2428
public function up(): void
2529
{
26-
$this->schema->create('love_reactions', function (Blueprint $table) {
30+
$this->schema->create((new Reaction())->getTable(), function (Blueprint $table) {
2731
$table->bigIncrements('id');
2832
$table->unsignedBigInteger('reactant_id');
2933
$table->unsignedBigInteger('reacter_id');
@@ -52,17 +56,17 @@ public function up(): void
5256
$table
5357
->foreign('reactant_id')
5458
->references('id')
55-
->on('love_reactants')
59+
->on((new Reactant())->getTable())
5660
->onDelete('cascade');
5761
$table
5862
->foreign('reacter_id')
5963
->references('id')
60-
->on('love_reacters')
64+
->on((new Reacter())->getTable())
6165
->onDelete('cascade');
6266
$table
6367
->foreign('reaction_type_id')
6468
->references('id')
65-
->on('love_reaction_types')
69+
->on((new ReactionType())->getTable())
6670
->onDelete('cascade');
6771
});
6872
}
@@ -74,6 +78,6 @@ public function up(): void
7478
*/
7579
public function down(): void
7680
{
77-
$this->schema->dropIfExists('love_reactions');
81+
$this->schema->dropIfExists((new Reaction())->getTable());
7882
}
7983
}

database/migrations/2018_07_25_000000_create_love_reactant_reaction_counters_table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14+
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter;
1415
use Cog\Laravel\Love\Support\Database\Migration;
1516
use Illuminate\Database\Schema\Blueprint;
1617

@@ -23,7 +24,7 @@ final class CreateLoveReactantReactionCountersTable extends Migration
2324
*/
2425
public function up(): void
2526
{
26-
$this->schema->create('love_reactant_reaction_counters', function (Blueprint $table) {
27+
$this->schema->create((new ReactionCounter())->getTable(), function (Blueprint $table) {
2728
$table->bigIncrements('id');
2829
$table->unsignedBigInteger('reactant_id');
2930
$table->unsignedBigInteger('reaction_type_id');
@@ -56,6 +57,6 @@ public function up(): void
5657
*/
5758
public function down(): void
5859
{
59-
$this->schema->dropIfExists('love_reactant_reaction_counters');
60+
$this->schema->dropIfExists((new ReactionCounter())->getTable());
6061
}
6162
}

database/migrations/2018_07_25_001000_create_love_reactant_reaction_totals_table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14+
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal;
1415
use Cog\Laravel\Love\Support\Database\Migration;
1516
use Illuminate\Database\Schema\Blueprint;
1617

@@ -23,7 +24,7 @@ final class CreateLoveReactantReactionTotalsTable extends Migration
2324
*/
2425
public function up(): void
2526
{
26-
$this->schema->create('love_reactant_reaction_totals', function (Blueprint $table) {
27+
$this->schema->create((new ReactionTotal())->getTable(), function (Blueprint $table) {
2728
$table->bigIncrements('id');
2829
$table->unsignedBigInteger('reactant_id');
2930
$table->unsignedBigInteger('count');
@@ -45,6 +46,6 @@ public function up(): void
4546
*/
4647
public function down(): void
4748
{
48-
$this->schema->dropIfExists('love_reactant_reaction_totals');
49+
$this->schema->dropIfExists((new ReactionTotal())->getTable());
4950
}
5051
}

src/Support/Database/Eloquent/Model.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ public function getConnectionName(): ?string
2727
{
2828
return Config::get('love.storage.database.connection');
2929
}
30+
31+
public function getTable(): string
32+
{
33+
return Config::get("love.storage.database.tables.{$this->table}")
34+
?? $this->table;
35+
}
3036
}

0 commit comments

Comments
 (0)