Skip to content

Commit a782a6b

Browse files
committed
feature: add subscription definition in database
1 parent 1d6bd85 commit a782a6b

File tree

6 files changed

+171
-14
lines changed

6 files changed

+171
-14
lines changed

app/Models/Subscription.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Database\Factories\SubscriptionFactory;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
9+
use Illuminate\Support\Carbon;
10+
use Illuminate\Support\Collection;
11+
12+
/**
13+
* @property-read int $id
14+
* @property string $name
15+
* @property array $rules
16+
* @property Carbon $created_at
17+
* @property Carbon $updated_at
18+
*
19+
* @property-read Collection $users
20+
*
21+
* @method static SubscriptionFactory factory(...$parameters)
22+
*/
23+
class Subscription extends Model
24+
{
25+
use HasFactory;
26+
27+
protected $fillable = [
28+
'name',
29+
'rules'
30+
];
31+
32+
protected $casts = [
33+
'rules' => 'array',
34+
];
35+
36+
public function users(): HasMany
37+
{
38+
return $this->hasMany(User::class);
39+
}
40+
}

app/Models/User.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Database\Factories\UserFactory;
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
78
use Illuminate\Database\Eloquent\Relations\HasMany;
89
use Illuminate\Foundation\Auth\User as Authenticatable;
910
use Illuminate\Notifications\Notifiable;
@@ -18,11 +19,13 @@
1819
* @property string $name
1920
* @property string $email
2021
* @property string $password
22+
* @property ?int $subscription_id
2123
* @property ?Carbon $email_verified_at
2224
* @property Carbon $created_at
2325
* @property Carbon $updated_at
2426
*
2527
* @property-read Collection $notes
28+
* @property-read Subscription $subscription
2629
*
2730
* @method static UserFactory factory(...$parameters)
2831
*/
@@ -75,6 +78,11 @@ class User extends Authenticatable
7578
'profile_photo_url',
7679
];
7780

81+
public function subscription(): BelongsTo
82+
{
83+
return $this->belongsTo(Subscription::class);
84+
}
85+
7886
public function notes(): HasMany
7987
{
8088
return $this->hasMany(Note::class);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\Subscription;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
/**
9+
* @extends Factory<Subscription>
10+
* @method Subscription create($attributes = [])
11+
*/
12+
class SubscriptionFactory extends Factory
13+
{
14+
/**
15+
* The name of the factory's corresponding model.
16+
*
17+
* @var string
18+
*/
19+
protected $model = Subscription::class;
20+
21+
/**
22+
* Define the model's default state.
23+
*
24+
* @return array
25+
*/
26+
public function definition()
27+
{
28+
return [
29+
'name' => $this->faker->name,
30+
'rules' => [
31+
'notes_maximum_amount' => array_rand([null, 100, 1000])
32+
],
33+
];
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateSubscriptionsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('subscriptions', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name', 80);
19+
$table->json('rules')->nullable();
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::dropIfExists('subscriptions');
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddSubscriptionIdToUsersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('users', function (Blueprint $table) {
17+
$table->foreignId('subscription_id')->after('password')->nullable()->constrained();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('users', function (Blueprint $table) {
29+
$table->dropColumn('subscription_id');
30+
});
31+
}
32+
}

database/seeders/DatabaseSeeder.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22

33
namespace Database\Seeders;
44

5+
use App\Models\Subscription;
56
use App\Models\User;
67
use App\Models\Note;
78
use Illuminate\Database\Seeder;
8-
use Illuminate\Support\Collection;
99
use Illuminate\Support\Facades\App;
1010
use Illuminate\Support\Facades\Hash;
1111

1212
class DatabaseSeeder extends Seeder
1313
{
14-
/**
15-
* Seed the application's database.
16-
*
17-
* @return void
18-
*/
19-
public function run()
14+
public function run(): void
2015
{
2116
$admin = new User();
2217
$admin->name = config('admin.name');
@@ -25,16 +20,30 @@ public function run()
2520
$admin->save();
2621

2722
if (! App::environment('production')) {
28-
User::factory(9)->create();
23+
$premiumSubscription = Subscription::factory()->create([
24+
'name' => 'Premium subscription',
25+
'rules' => ['notes_maximum_amount' => null]
26+
]);
2927

30-
/** @var Collection $usersId */
31-
$usersId = User::query()
32-
->whereNot('id', $admin->getKey())
28+
$admin->subscription()->associate($premiumSubscription)->save();
29+
30+
$freeSubscription = Subscription::factory()->create([
31+
'name' => 'Free subscription',
32+
'rules' => ['notes_maximum_amount' => 100]
33+
]);
34+
35+
User::factory(100)->create([
36+
'subscription_id' => $freeSubscription->getKey()
37+
]);
38+
39+
User::query()
3340
->select('id')
3441
->get()
35-
->pluck('id');
36-
37-
Note::factory(200)->create(['user_id' => $usersId->random()]);
42+
->each(function (User $user){
43+
Note::factory(50)->create([
44+
'user_id' => $user->getKey()
45+
]);
46+
});
3847
}
3948
}
4049
}

0 commit comments

Comments
 (0)